Rogue Wave Views
Foundation Package API Reference Guide
Product Documentation:

Rogue Wave Views
Documentation Home
Classes | Macros
smartptr.h File Reference
#include <ilviews/macros.h>
#include <stdlib.h>

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...
 

Detailed Description

Library: views
Declaration of the reference counting management classes, IlvSmartData and IlvSmartPointer,

Macro Definition Documentation

#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)

See also
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

  • Regular pointers versus Smart Pointers
    Assigning and deleting a smart pointer is fast, but slower than assigning and deleting a regular pointer. As a consequence, you should always make sure that you use the appropriate type of pointer. Here is a simple rule you can follow to determine which kind of pointer should be used in given situation: When a pointer has to be declared inside a pair of braces ({}), it should be a smart pointer. When it has to be declared inside a pair of parentheses (()), it should be a regular pointer.
    Here is an example:
    1 class MyWindow
    2 : public IlvSmartData {
    3  ...
    4 };
    5 
    6 IlvPointerClass(pMyWindow, MyWindow, MYPACKAGE); // defines the class pMyWindow
    7 
    8 class MyDocument {
    9  pMyWindow myWindow; // A smart pointer, because we need the object
    10  // to remain valid as long as the whole object
    11  // is valid.
    12 
    13  void setWindow(MyWindow* window) {
    14  // A regular pointer:
    15  // we don't need to change the reference count when
    16  // passing the object as an argument.
    17  pMyWindow tmpW = window; // A smart pointer:
    18  // we want the pointer to remain valid within
    19  // the method's scope.
    20  for (MyWindow* w = window; w != 0; w = w->next()) {
    21  // A regular pointer: w remains valid within this scope.
    22  // and we don't need to change the counters.
    23  }
    24  myWindow = window;
    25  }
    26 };
  • 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>.

See also
IlvSmartData, IlvDerivedPointerClass.

© Copyright 2016, Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave is a registered trademark of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.