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
8
votes
4 answers

Get notification of object disposal/destruction

I need a way to track instances of various classes, without those classes having any knowledge that they are being tracked. Essentially, I have a class factory which creates instances and hands them off to another thread. Once that thread completes…
Tim Long
  • 13,508
  • 19
  • 79
  • 147
8
votes
1 answer

Lifetime management with Google Guice

Is there a recommended pattern for shutting down / closing objects created with Guice? The lifecycle I'm aiming for is: Prepare a Guice Module Create an injector Use the injector through your code to obtain objects…
ripper234
  • 222,824
  • 274
  • 634
  • 905
7
votes
4 answers

NOT the most important const.. but what is this?

This outputs F~ but I was expecting ~F #include struct Foo { int _x; operator const int & () const {return _x;} ~ Foo () {std :: cout << "~";} }; void foo (const int &) { std :: cout << "F"; } int main () { foo (Foo…
spraff
  • 32,570
  • 22
  • 121
  • 229
7
votes
2 answers

Safe way in Delphi for a Form to distribute interface objects tied to its lifetime?

I have a Delphi Form that provides the functionality behind an interface object that other parts of the code get references too via a property belonging to the Form. I can't delegate the interface functionality to a child object because too much of…
Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
7
votes
1 answer

Arrays and implicit-lifetime object creation

Some types are defined by standard as implicit-lifetime types, and arrays are among them. Some functions implicitly create objects with implicit-lifetime (malloc etc are among them), with a list of operations that implicitly create objects with…
7
votes
1 answer

How does std::future affects the lifetime of an associated std::packaged_task?

I have an std::packaged_task containing a lambda that captures a variable by copy. When this std::packaged_task is deleted, I would expect the variable living inside the lambda to be destructed, but I noticed that if I get the associated std::future…
7
votes
2 answers

Is it safe to wait for asynchronous work by joining in the destructor?

Suppose I have a class that may run some code asynchronously, and that asynchronous code uses that class instance to do things like call member functions, read data members, etc. Obviously the class instance must outlive the background thread in…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
7
votes
1 answer

What is the order of destruction of objects in VBScript?

In what order are objects in a .vbs destroyed? That is, given these globals: Set x = New Xxx Set y = New Yyy I'm interested in answers to any of the following. For instances of classes implemented in the .VBS, in what order will Class_Terminate be…
bacar
  • 9,761
  • 11
  • 55
  • 75
7
votes
1 answer

No dangling reference for std::min in libc++

It is well known (or it should be) that binding the result of std::min to a const reference is a very bad idea, whenever one of the arguments of std::min is a rvalue, since const reference binding is not propagated through function return. So the…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
7
votes
3 answers

What wording in the C++ standard allows static_cast(malloc(N)); to work?

As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*-to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a…
ben
  • 1,994
  • 12
  • 20
7
votes
3 answers

C++ Using a reference to the variable being defined

Is the following code valid C++, according to the standard (discounting the ...s)? bool f(T& r) { if(...) { r = ...; return true; } return false; } T x = (f(x) ? x : T()); It is known to compile in the GCC versions…
ShdNx
  • 3,172
  • 5
  • 40
  • 47
7
votes
3 answers

What's the rationale of the exceptions of temporary object lifetime expansion when bound to a reference?

In 12.2 of C++11 standard: The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except: A temporary bound to a…
updogliu
  • 6,066
  • 7
  • 37
  • 50
7
votes
2 answers

__del__ method being called in python when it is not expected

I am new to python and have been working through the examples in Swaroop CH's "A Byte of Python". I am seeing some behavior with the __del__ method that is puzzling me. Basically, if I run the following script (in Python 2.6.2) class Person4: …
ELee
  • 83
  • 1
  • 5
6
votes
2 answers

The lifetime of a reference with regard to its target

In order to stem the argument going on in the comments of an answer I gave recently, I'd like some constructive answers to the following questions: Is a reference's lifetime distinct from the object it refers to? Is a reference simply an alias for…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
6
votes
1 answer

Clarification about object lifetime in destructor

Another question cites the C++ standard: 3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or — the storage which the object occupies is reused…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119