Namespaces
Variants
Actions

Cleanup stack support for non-CBase classes

Jump to: navigation, search
Article Metadata

Article
Created: vin2ktalks (20 Mar 2007)
Last edited: hamishwillee (20 Jul 2012)

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.


Example: RPointerArray

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);
theArray->ResetAndDestroy();
}
 
RPointerArray<HBufC> myArray;
...
 
CleanupStack::PushL(TCleanupItem(PointerArrayCleanup, &myArray));
...
CleanupStack::PopAndDestroy(); // will call PointerArrayCleanup(&myArray)
This page was last modified on 20 July 2012, at 09:22.
94 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved