Cleanup stack support for non-CBase classes

The cleanup stack supports other types of cleanup using CleanupStack::PushL().

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 cleanup. You can push:

  • a pointer to any type of object. This is an object of type TAny*. The cleanupstack destroys this type of object with a call to User::Free() on the pointer pushed. This is less powerful than standard cleanup, because it frees memory and does not call the C++ destructor.

    You use the CleanupStack::PushL(TAny* aPtr); overload.

  • a pointer to an object together with a specific cleanup operation for that object. The pointer and the cleanup operation are contained in a TCleanupItem object.

    You use the CleanupStack::PushL(TCleanupItem anItem) overload.

    You must understand that the TCleanupItem object does not go on the cleanupstack. The pointer to the object that is contained within the TCleanupItem object goes on the cleanupstack. For example, if you created a TCleanupItem called myCleanupItem, you must never call CleanupStack::Pop(myCleanupItem);

Some utility functions are provided that make construction of a suitable TCleanupItem easy. See How to clean up non-CBase classes.