parts/4.4.Lifetime.md

4.4 Object Lifetime

In principle, the lifetime of an object (recursively including subobjects) begins immediately after its constructor completes creating the object and ends immediately before its destructor is called.

Accessing an object outside of its lifetime is undefined behaviour [EWF].

C++ distinguishes references from objects. A reference has a lifetime that begins immediately after it is initialized and ends when the scope or the object containing it ends. If the lifetime of an object designated by a reference ends before the lifetime of the reference ends, the reference is dangling and further access to its referent is undefined behaviour.

An object of implicit lifetime type is implicitly created when declared or allocated. For class types that are not implicit lifetime types at least one constructor must complete for the lifetime of the object to begin.

Each object has one of the following types of storage durations which influences its lifetime: - static - includes global and static variables; once lifetime has started the object is guaranteed to exist until the program ends. - thread - once lifetime has started, exists on a per-thread basis until the containing thread ends. - automatic - includes non-static local variables and temporary objects; its context defines its lifetime. - dynamic - storage for objects is acquired from dynamic memory allocation and its lifetime is explicitly controlled.

The lifetime of an object can be prematurely terminated by explicitly - calling its destructor - reusing its storage in a placement new expression - releasing its storage Reusing or releasing an object’s storage does not call its destructor: this must be done explicitly. The correct behaviour of a program often depends on the destructor being invoked for each object of class type. Improper management of objects with dynamic storage duration can cause memory leaks.

TO Do: verify that this clause identifies issues raised in: 6.22.InitializationOfVariables-LAV.md 6.33.DanglingReferencesToStackFrames-DCM.md 6.38.DeepVsShallowCopying-YAN.md 6.39.MemoryLeakAndHeapFragmentation-XYL.md 6.61.ConcurrentDataAccess-CGX.md 6.63.ProtocolLockErrors-CGM.md 6.65.ModifyingConstants-UJO.md