Questions tagged [object-lifetime]

The object lifetime (or life cycle) of an object in object-oriented programming is the time between an object is created (also known as instantiation or construction) till the object is no longer used and then destructed or freed.

In typical case, the process is as follows:

  1. Memory allocation and binding: Calculate the size of the object (usually the alligned size of each member of the object), allocating memory space with the size of an object plus the growth later, if possible to know in advance and binding methods.
  2. Constructor call and execution: First the constructor of superclass(es) are called, then the constructor of the object.
  3. Object use.
  4. Destructor call and execution: First the destructor of the object are called, then the destructor(s) of the superclass(es).
  5. Memory deallocation and unbinding.
402 questions
4
votes
2 answers

Why is an 'invisible' object not instantly collected?

I just read this article: The Truth About Garbage Collection In section "A.3.3 Invisible" it is explained how and when an object gets into the invisible state. In the below code, the object assigned to the variable foo will become invisible after…
yas4891
  • 4,774
  • 3
  • 34
  • 55
4
votes
4 answers

.NET - Finalizers and exit(0)

I have a .NET C# / C++ app which uses a call to exit(0) (from ) in a thread in order to terminate. The strange part is, under some circumstances, the finalizers of the managed objects are called right after the call to exit, and in other…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
4
votes
1 answer

Is Rust-style ownership and lifetimes possible without Rust-style borrow checking?

Would it be possible for a programming language to consistently have Rust-style ownership and lifetimes (for automatic memory management) while dropping the requirement that only one mutable reference to a piece of data can exist at any time (used…
4
votes
1 answer

What is the lifetime of a temporary object bound to a reference in a new-initializer?

From [class.temporary] of the Working Draft, Standard for Programming Language C++: (6.12) — A temporary bound to a reference in a new-initializer ([expr.new]) persists until the completion of the full-expression containing the…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
4
votes
1 answer

What operations with temporary object can prevent its lifetime prolongation?

As you know in C++, if a temporary object is bound to a local reference, then the lifetime of the temporary is extended till the end of the scope. But what happens if one performs a cast with a temporary or some other operation, e.g.: #include…
Fedor
  • 17,146
  • 13
  • 40
  • 131
4
votes
2 answers

G++-11 destruction order changed from G++9

We have following code(it's more complicated ofc, I tried to make a minimal example). #include #include #include #include template struct use_type { use_type(T& v) : value(v) {} T&…
ForEveR
  • 55,233
  • 2
  • 119
  • 133
4
votes
6 answers

Is it possible to implement a DefaultIfNull function in C++?

Disclaimer: This is rather more out of curiosity than for a lack of other solutions! Is it possible to implement a function in C++ that: gets passed a pointer of type T either returns a reference-like-thing to the object pointed to by T or, if the…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
4
votes
1 answer

Return value optimization for string_view inside shared_ptr

It's hard to put into words so I will directly jump into a semi-pseudo-code. I have a download function (http GET), that is being called many many times inside my main code. std::string download_data(){ std::shared_ptr response =…
Max Paython
  • 1,595
  • 2
  • 13
  • 25
4
votes
2 answers

Delphi FreeAndNil: Looking for an alternate implementation

NOTE: Bear with me, I feel a little "flame grilled" due to some discussions over here and here and some issues I reported here and here. Some background Ye olde (pre 10.4) FreeAndNil looked like this: FreeAndNil(var SomeObject) The new and fresh…
4
votes
3 answers

Understand C++ pointer lifetime / zombie pointers

After watching CppCons Will Your Code Survive the Attack of the Zombie Pointers? I'm a bit confused about pointer lifetime and need some clarification. First some basic understanding. Please correct me if any comments are wrong: int* p = new…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
4
votes
2 answers

reusing object's space by another object

I was looking to re-use allocated space within the base class from this pointer and C++ Standard does not approve. However, the wording of the standard seems to be wrong. It puts a condition "and before the storage which the object occupied is…
minex
  • 329
  • 1
  • 5
4
votes
3 answers

Problems with c++ lifetime extension

I've tried to understand the semantics of c++ temporary objects lifetime extension. I've tried to simulate simple situation and was a bit surprised. Below I'm providing my code. #include struct C { C(const int new_a) { a = new_a;…
alex
  • 53
  • 4
4
votes
2 answers

Is calling destructor from a catch block in constructor safe?

In my constructor, I have to destroy any remaining resources if any code in it throws. I'd like to avoid writing duplicate code so I just call the destructor in the catch block which than frees any resource that has been created. Is this safe? I'm…
user7321642
4
votes
1 answer

Chapel : Understanding lifetime of managed classes with zip and user-defined iterators

I'm trying to understand the lifetime of an owned class, when being used in a user-defined iterator. Consider the following code : var a = new owned C(); var b = new owned C(); a.i = 2; forall (a1,b1) in zip(a,b) { b1 = a1; } forall (a1,b1) in…
4
votes
1 answer

Indirect call of virtual function from Destructor

Let me make this disclaimier : I have clear understanding of virtual function call in Constructor or Destructor. In the below code I am trying to avoid virtual destructor ONLY FOR EXPERIMENTAL purpose. Now my question is: In main the call to Destroy…
vrbilgi
  • 5,703
  • 12
  • 44
  • 60