Smart Pointers

Server internally manipulates smart pointers to support relations defined between server objects. Smart pointers are similar to ordinary pointers, except that they allow the implicit destruction of an object that is no longer referenced —an object which is no longer pointed to. The main characteristic of smart pointers is that they act as garbage collectors, in that they clean up after themselves.

How Do Smart Pointers Work?

Smart pointers maintain a reference counter for the object they point to. This counter is incremented each time a new smart pointer references the object. It is decremented each time a smart pointer no longer points to the object.

When the reference counter of an object equals zero, the object is deleted and the allocated resources are automatically reclaimed.

How Smart Pointers Work

Benefits of Smart Pointers

  • When using smart pointers, you do not have to destroy objects explicitly by calling the C++ delete function.

  • Smart pointers help maintain the referential integrity of server objects by:

    • ensuring that a given object is not deleted as long as at least one smart pointer points to it,

    • destroying automatically objects that are no longer pointed to,

    • preventing access to objects that have already been deleted.

  • Smart pointers optimize memory management: by allowing automatic reclaim of memory, they avoid memory leaks, which cause incontrolled increase of application size.

Note

When using smart pointers to a given server object, you should not delete this object explicitly. If you are new to Server, do not use smart pointers and ordinary pointers to the same server object, except when you are certain of the life expectancy of the object.