Smart pointers are used instead of simple pointers for working with objects.
A smart pointer is an abstract data type that simulates a pointer, providing
additional features for accessing objects and managing memory at run-time, for
example, automatic destroying of unused objects, throwing of exceptions, inspecting
owners, or bounds checking. These additional features are intended to reduce
issues caused by the misuse of pointers in the program while retaining efficiency.
Smart pointers typically keep track of objects and they help with memory management
at run-time.
Using smart pointers helps avoid issues typically caused by the misuse of raw
pointers:
Memory leaks — The program stores an object in its own dynamic memory, but does
not have a pointer that stores the address of this object — the object exists, but
nothing uses it.
Dangling of pointers — A pointer stores the address of an object that is
destroyed in the program — the pointer refers to an object that does not
exist.
Incorrect exception throwing — The pointer is NULL or refers to an object of
another type while the program throws an exception and someone tries to de-reference it
— the exception operates on an incorrect pointer.
Out of scope — The pointer gets out of the scope of available memory.
Smart pointer technology is used in application development. Every database object
class is inherited from the OdRxClass, which implements the Runtime Type Identification
mechanism that provides the ability to dynamically identify the class and get various
information about it at run-time. Any database object can be created as an instance of
OdRxObject by calling the static OdRxClass::createObject() function. This call creates the
object and returns a smart pointer of specific type for the object.
For example, to create a level object and acquire a smart pointer
for it: