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.
367 questions
135
votes
2 answers

Is this object-lifetime-extending-closure a C# compiler bug?

I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the…
Ani
  • 103,292
  • 21
  • 241
  • 294
75
votes
10 answers

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have used std::memcpy for a long time to bitwise swap…
R Sahu
  • 196,807
  • 13
  • 136
  • 247
74
votes
2 answers

Object destruction in C++

When exactly are objects destroyed in C++, and what does that mean? Do I have to destroy them manually, since there is no Garbage Collector? How do exceptions come into play? _(Note: This is meant to be an entry to [Stack Overflow's C++…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
69
votes
3 answers

Exact moment of "return" in a C++-function

It seems like a silly question, but is the exact moment at which return xxx; is "executed" in a function unambiguously defined? Please see the following example to see what I mean (here live): #include #include #include…
ead
  • 27,136
  • 4
  • 67
  • 108
62
votes
10 answers

AppDomain and MarshalByRefObject life time : how to avoid RemotingException?

When a MarshalByRef object is passed from an AppDomain (1) to another (2), if you wait 6 mins before calling a method on it in the second AppDomain (2) you will get a RemotingException : System.Runtime.Remoting.RemotingException: Object [...] has…
Guillaume
  • 12,247
  • 3
  • 37
  • 47
47
votes
8 answers

call to pure virtual function from base class constructor

I have a base class MyBase that contains a pure virtual function: void PrintStartMessage() = 0 I want each derived class to call it in their constructor then I put it in base class(MyBase) constructor class MyBase { public: virtual void…
41
votes
1 answer

Lifetime of lambda objects in relation to function pointer conversion

Following this answer I'm now wondering what the rules are for the lifetime of lambdas and how the relate to the lifetime of function pointers which are created by automatic conversion. There are several questions about the lifetime of lambdas (e.g.…
Flexo
  • 82,006
  • 22
  • 174
  • 256
38
votes
1 answer

Destruction of return value on destructor exception

I have the following code: #include #include struct ok { int _n; ok(int n) : _n(n) { std::cerr << "OK" << n << " born" << std::endl; } ~ok() { std::cerr << "OK" << _n << " gone" << std::endl; } }; struct…
Evgeny
  • 1,959
  • 17
  • 29
35
votes
3 answers

What is the order of destruction of function arguments?

If some function f with parameters p_1, ..., p_n of types T_1, ..., T_n respectively is called with arguments a_1, ..., a_n and its body throws an exception, finishes or returns, in what order are the arguments destroyed and why? Please provide a…
jotik
  • 14,982
  • 9
  • 48
  • 106
33
votes
5 answers

How to deal with run-time parameters when using lifetime scoping?

Warning, long post ahead. I've been thinking a lot about this lately and I'm struggling to find a satisfying solution here. I will be using C# and autofac for the examples. The problem IoC is great for constructing large trees of stateless services.…
Kugel
  • 17,248
  • 12
  • 64
  • 100
33
votes
2 answers

Difference between IOptionsMonitor vs. IOptionsSnapshot

According to this answer, IOptionsMonitor is registered in DI container as singleton and is capable of detecting changes through OnChange event subscription. It has a CurrentValue property. On the other hand, IOptionsSnapshot is registered as scoped…
30
votes
1 answer

What does it mean for an object to exist in C++?

[class.dtor]/15 reads, emphasis mine: Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended (3.8). However, as far as I can tell, this…
Barry
  • 247,587
  • 26
  • 487
  • 819
26
votes
3 answers

Is it ok to return default argument's value by const reference?

Is it ok to return default argument's value by const reference like in the examples below: https://coliru.stacked-crooked.com/a/ff76e060a007723b #include const std::string& foo(const std::string& s = std::string("")) { return s; } int…
26
votes
4 answers

memcpy/memmove to a union member, does this set the 'active' member?

Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy, it copies from the address of a plain old uint32_t, which is not contained within a union. Also, I am copying (via memcpy) to a…
Aaron McDaid
  • 24,484
  • 9
  • 56
  • 82
23
votes
3 answers

Is it legal to call member functions after an object has been explicitly destroyed but before its memory was deallocated?

I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being called after the object has been destroyed but…
1
2 3
24 25