An object shall not be accessed outside of its lifetime
An object shall not be accessed outside of its lifetime.
The lifetime of an object begins when it is created by its constructor. The lifetime ends when the object is deleted. Accessing a variable before its construction or after its destruction can lead to undefined behavior. Depending on the context, many operations might inadvertently access an object outside its lifetime. Examples of such operations include:
Noninitialized pointer: You might inadvertently access a pointer before assigning
an address to it. This operation accesses an object before its lifetime and results in
accessing an unpredictable memory location. The best practice is to initiate a pointer
by using nullptr during its declaration.
Noninitialized variable: You might inadvertently read a variable before it is initialized. This operation accesses an object before its lifetime and results in reading a garbage value that is unpredictable and useless. The best practice is to initiate a variable during its declaration.
Use of previously deallocated pointer: You might access the dynamically allocated
memory of a pointer after deallocating the memory. Trying to access this block of
memory accesses an object after its lifetime and results in unpredictable behavior or
even a segmentation fault. To address this issue, set the deallocated pointer to
nullptr, and then to check if a pointer is
nullptr before accessing it. Alternatively, use a
std::unique_ptr instead of a raw pointer. Because you do not need
to deallocate the allocated memory for a std::unique_ptr
explicitly, you can avoid inadvertently accessing the deallocated memory.
Pointer or reference to stack variable leaving scope: You might assign a nonlocal pointer to a local object. For instance:
A nonlocal or global pointer is assigned to a variable that is local to a function.
A passed-by-reference function parameter, such as a pointer, is assigned to a variable that is local to a function.
A pointer data member of a class is assigned to a variable that is local to a function.
Once the local variable goes out of scope, their corresponding memory blocks might hold garbage or unpredictable values. Accessing pointers to these memory locations accesses an object after its lifetime and might result in undefined or unpredictable behavior. The best practice is to not assign nonlocal pointers to local objects.
Modifying object with temporary lifetime: You might attempt to modify a temporary object returned by a function call. Modifying temporary objects is an undefined behavior that might lead to abnormal program termination depending on the hardware and software that you use. The best practice is to assign the temporary objects in local variables, and then modifying the local variables.
Avoid operations that might access an object outside of its lifetime.
Polyspace® checks for these scenarios where an object might be accessed outside of its lifetime:
Noninitialized pointer: Polyspace flags a pointer if it is not assigned an address before it is accessed.
Noninitialized variable: Polyspace flags a variable if it is not initialized before its value is read.
Use of previously deallocated pointer: Polyspace flags an operation where you access a block of memory after deallocating
the block, for instance, by using the free() function or the
delete operator.
Pointer or reference to stack variable leaving scope: Polyspace flags a local variable when a pointer or reference to it leaves its scope. For example, a local variable is flagged when:
A function returns a pointer to the local variable
A global pointer is pointed to the local variable
A pass-by-reference function parameter, such as a pointer, is pointed to the local variable
A pointer data member of a class is pointed to the local variable
Accessing object with temporary lifetime: Polyspace flags an operation where you access a temporary object that is returned by a function call.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Basic concepts |
| Category: Required, Non-automated |