Memory Management

FDO API

 
Memory Management
 
 
 

Some FDO functions (for example, the Create methods) allocate memory when they are called. This memory needs to be freed to prevent memory leaks. All destructors on FDO classes are protected, so you must call a Release() function to destroy them (thus freeing their allocated memory). Each class inherits from the FdoIDisposable class, which defines the Release() method and the AddRef() method.

In addition, these classes are reference counted, and the count is increased (by AddRef()) when you retrieve them through a Get function. After finishing with the object, you need to release it (just as with COM objects). The object is destroyed only when the reference count hits 0. Two macros are defined to help in the use of the Release() and AddRef() methods.

FDO_SAFE_RELEASE (*ptr)

If the “*ptr” argument is not null, FDO_SAFE_RELEASE calls the release() method of the object pointed to by the “*ptr” argument and then sets the local pointer to the object to NULL. The macro definition is #define FDO_SAFE_RELEASE(x) {if (x) (x)->Release(); (x) = NULL;}.

FdoFeatureClass* pBase = myClass->GetBaseClass(); 
... 
// Must release reference added by GetBaseClass when done. 
FDO_SAFE_RELEASE(pBase); 

FDO_SAFE_ADDREF (*ptr)

If the “*ptr” argument is not null, FDO_SAFE_ADDREF calls the AddRef() method of the object pointed to by the “*ptr” argument. The macro definition is #define FDO_SAFE_ADDREF(x) ((x != NULL) ? (x)->AddRef(), (x): (NULL)).

  • return FDO_SAFE_ADDREF(value)returns NULL if value equals NULL or increments the reference count of the object that value points to and returns value.
  • m_list[index] = FDO_SAFE_ADDREF(value) assigns NULL to the array entry if value is NULL or increments the reference count of the object that value points to and assigns value to the array entry.

FdoPtr

An FdoPtr smart pointer is provided to help manage memory. You wrap an FDO object in a FdoPtr. The requirement is that the object’s type must inherit from FdoIDisposable. The object is then released automatically when the FdoPtr goes out of scope. The following code illustrates how to use FdoPtr:

FdoPtr<FdoFeatureClass> pBase = myClass->GetBaseClass(); 
... 
// No need to call FDO_SAFE_RELEASE.
// Before it is destroyed, pBase calls Release() on the FdoFeatureClass object
NoteIf, for some reason, you wanted to use FDO_SAFE_RELEASE on an FdoPtr, you would have to use an FdoPtr method to get a pointer to the object that FdoPtr wraps and pass that pointer to FDO_SAFE_RELEASE.

You can use FdoPtr for your own classes by inheriting from the abstract class FdoIDisposable and providing an implementation for the Dispose() method (typically delete this;).

FdoPtr Typedefs

Typedefs are provided that define identifiers representing Fdo classes wrapped byFdoPtr. An example is typedef FdoPtr<FdoClass> FdoClassP.