rwlogo

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 ilviews/base/smartptr.h] 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:
    class MyWindow : public IlvSmartData {
    ...
    };
    IlvPointerClass(pMyWindow, MyWindow, MYPACKAGE); // defines the class pMyWindow
    class MyDocument {
    pMyWindow myWindow; // a smart pointer, because we need the object
    // to stay valid as long as we exist.
    void setWindow(MyWindow* window) {
    // a regular pointer:
    // we don't need to touch the reference count when
    // passing the object as an argument.
    pMyWindow tmpW=window; // a smart pointer:
    // we want the pointer to stay valid within
    // the method's scope.
    for (MyWindow* w=window; w!=0; w=w->next()) {
    // a regular pointer:
    // w will stay valid within this scope.
    // and we don't need to keep incrementing/decrementing
    // the counters.
    }
    myWindow=window;
    }
    };
  • 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 2014, 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.