Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Cleanup stack support for non-CBase classes
The discussion of clean up so far has assumed objects to be cleaned up are derived from CBase, with clean up by invoking delete. Other classes need explicit cleanup support to be provided by the programmer.
The cleanup stack supports other types of object by two further overloaded versions of CleanupStack::PushL(). You may push:
- a TAny*: such objects are destroyed by invoking User::Free() on the pointer pushed — note that this is less powerful: it simply frees the memory, without calling the C++ destructor
- a TCleanupItem: an object of this type encapsulates a pointer to the object to be pushed, and a pointer to a function that provides cleanup for that object.
Some utility functions are provided that make construction of a suitable TCleanupItem easy.
An instance of an RPointerArray can be put on the CleanupStack and safely destroyed by writing a suitable static function, which is then associated with the array in a TCleanupItem. In this example the array contains HBufC descriptors.
// static cleanup function
static void PointerArrayCleanup( TAny* aArray )
RPointerArray<HBufC>* theArray = static_cast<RPointerArray<HBufC>*> (aArray);
CleanupStack::PopAndDestroy(); // will call PointerArrayCleanup(&myArray)