Classes | |
class | IlvSmartData |
Base class for reference counting. More... | |
Macros | |
#define | IlvDerivedPointerClass(SmartClass, BaseSmartClass, DataClass, EXPORT) |
#define | IlvPointerClass(SmartClass, DataClass, EXPORT) |
Base class for pointers that handle a reference counter for the objects they point to. More... | |
Library: views
Declaration of the reference counting management classes, IlvSmartData
and IlvSmartPointer
,
#define IlvDerivedPointerClass | ( | SmartClass, | |
BaseSmartClass, | |||
DataClass, | |||
EXPORT | |||
) |
This macro defines subclasses of IlvSmartPointer
that point to subclasses of a base reference counting class. For example, if you define a subclass MyDialogWindow: public MyWindow
, you must use the following macro to be able to use objects of type pMyDialogWindow
in the same way as you would use a MyDialogWindow*
object: IlvDerivedPointerClass(pMyDialogWindow,pMyWindow,MyDialogWindow,MYPACKAGE)
IlvSmartPointer
. #define IlvPointerClass | ( | SmartClass, | |
DataClass, | |||
EXPORT | |||
) |
Base class for pointers that handle a reference counter for the objects they point to.
All the objects of the class IlvSmartData
that are allocated dynamically must be referenced by at least one SmartPointer
object to be valid.
Smart pointers handle a reference counter for the objects they point to and destroy these objects when they are no longer visible. The use of smart pointers avoids memory leaks and greatly simplifies and secures memory management.
You handle a smart pointer pA
to an object of class A
exactly like an A*
object. Some compilers are not able to analyze correctly the pA::operator A*()
, thus requiring that you cast the smart pointer into a pointer explicitly.
Guidelines
{}
), it should be a smart pointer. When it has to be declared inside a pair of parentheses (()
), it should be a regular pointer.Cycles
Reference counters maintained by smart pointers often generate reference cycles where objects reference each other in a circular way. The objects involved in reference cycles are never deleted because they are always pointed to by another object and, as a consequence, their reference counter is always positive. One solution to overcome this problem is to break the loop explicitly when you know that some objects will no longer be needed. A better solution, however, is to organize the data as a hierarchical structure that specifies which object is the parent and which object is the child. In a hierarchical structure, children should point to their parent via regular pointers instead of smart pointers.
In the above example, a pointer to MyDocument
in MyWindow
should be MyDocument*
instead of IlvSmartPointerTo<MyDocument>
.
IlvSmartData
, IlvDerivedPointerClass
.