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
9
votes
1 answer

Why does the variable not live long enough?

Consider this function that should return the file extension of a given Path. pub fn get_extension<'a>(path: &'a Path) -> Option<&'a str> { let path_str = path.as_str().unwrap(); let ext_pos = regex!(".[a-z0-9]+$").find(path_str); match…
Christoph
  • 26,519
  • 28
  • 95
  • 133
9
votes
2 answers

Who owns wrapped streams (e.g. TextWriter) in .NET?

I've recently encountered an error "ObjectDisposedException: Cannot access a closed Stream" [ObjectDisposedException: Cannot access a closed Stream.] System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402 …
user2864740
  • 60,010
  • 15
  • 145
  • 220
9
votes
1 answer

Are locals destroyed before or after evaluation of a function return value?

I am thinking of making a class which represents ownership of a synchronization primitive, something like this: class CCriticalSectionLock { public: CCriticalSectionLock( CCriticalSection &cs ) : cs( cs ) { cs.Enter(); } …
Kevin
  • 1,179
  • 7
  • 18
9
votes
6 answers

Does destroying and recreating an object make all pointers to this object invalid?

This is a follow-up to this question. Suppose I have this code: class Class { public virtual method() { this->~Class(); new( this ) Class(); } }; Class* object = new Class(); object->method(); delete object; which is a…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
0 answers

Is it possible to destroy and re-create an object this way?

Possible Duplicate: Can I get a fresh start in C++ without failing again? Consider T* o = new(T()), where T has a copy constructor defined. Also suppose expression new uses the default ::operator new() To re-use the memory allocated for o,…
Martin
  • 9,089
  • 11
  • 52
  • 87
8
votes
2 answers

Prevent temporary from extending its lifetime?

This may be impossible, but I was wondering if it was possible to keep a temporary from ever lasting past its original expression. I have a chain of objects which point to parent objects, and a member function which will create a child object, a…
zounds
  • 773
  • 8
  • 17
8
votes
4 answers

Warning C4172: Returning a reference to const std::string bound to a local variable. How safe is it?

I was just building one of our projects at work and I see a new function was added: const std::string& ClassName::MethodName() const { return ""; } The compiler gives a warning: Warning C4172: returning address of local variable or temporary I…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
8
votes
2 answers

What is expected lifetime of std::intializer_list object in C++14?

Please consider this simplified c++14 program: #include struct A { A() { std::cout << "A() "; } ~A() { std::cout << "~A() "; } }; int main() { auto l = std::initializer_list{A()}; std::cout << ".…
Fedor
  • 17,146
  • 13
  • 40
  • 131
8
votes
3 answers

C++ lifetime of union member

In the current version of the C++ standard draft, [basic.life]/1 states: The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and,…
user42768
  • 1,951
  • 11
  • 22
8
votes
1 answer

At what point does the lifetime of a trivial type created by placement-new start?

During a dive into dynamic memory, it occurred to me it appears contradictory how trivial types begin their lifetime. Consider the snippet void* p = ::operator new(sizeof(int)); // 1 // 2 new (p) int; // 3 When does the int start its…
Passer By
  • 19,325
  • 6
  • 49
  • 96
8
votes
3 answers

What's the point of temporary bound to a member lifetime statement in C++ Standard?

In this question user Happy Mittal quotes section 12.2.5 of C++03 Standard: A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. How can that be useful anyway? I mean once the…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
2 answers

Passing the "this" pointer to other class/function in destructor

Is it legal C++ to create a worker-object on the stack in the destructor of some master-object and pass the this pointer of the master-object to the helper-object? The helper-object would then also call member functions of the master-object or…
levzettelin
  • 2,600
  • 19
  • 32
8
votes
2 answers

Can the storage of trivially copyable objects be safely reallocated with realloc?

I know that trivially copyable objects can safely be copied my malloc into an appropriate storage location1 and that the destination object will have the same value as the source. Is this also possible with realloc? That is, if realloc some storage…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
8
votes
2 answers

Does guaranteed copy elision work with function parameters?

If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the…
Maël Nison
  • 7,055
  • 7
  • 46
  • 77
8
votes
1 answer

Implicit destructor execution in function calling

I'm wondering what the standard says about the following piece of code. Can string destructor of temporary object be executed before calling printPointer? p.s. VS2010 compiler doesn't complain about this code and works correctly. void…
skap
  • 320
  • 3
  • 16