Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



Destructor requirements

For C type classes, cleanup invokes the destructor. For other types of object, other actions may be required.

The destructor for C type classes must be coded to release all resources owned by the object. By definition, cleanup may occur on partially constructed objects: the destructor may not, therefore, assume that all conditions for a fully-constructed object hold in these circumstances. A typical situation to watch for is calling a member function on a pointer that is initialised in the ConstructL(), as the pointer could be NULL in failure cases.

Resources will usually be indicated by pointers or handles. It is important that such pointers or handles are null when there is no resource allocated, and that the destructor test for a non-null value before destroying them.

This behaviour is facilitated in CBase-derived classes, because their memory is guaranteed to be initially set to binary zeroes when they are first allocated.

Programmers must, however, take particular care with pointers which are used for owned objects which are repeatedly allocated and de-allocated throughout an object’s lifetime. When such owned objects are de-allocated, the pointer should always immediately be set to null.


See also

Advanced Cleanup.