Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

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



How to pop and destroy

In general, it is recommended that any objects which are accessed only through an automatic pointer are pushed onto the cleanup stack immediately, before being used, and only popped before they must be destroyed.

You can use the single function CleanupStack::PopAndDestroy() to both pop the object from the cleanup stack and destroy it. This operation is the usual thing to do when an object on the cleanup stack has been finished with.

void doExampleL()
 {
 // allocate and leave if could not
 CExample* myExample = new (ELeave) CExample;

 // this cannot leave - no protection needed
 myExample->iInt = 5;
   // do something that might leave, protected by cleanup stack
 CleanupStack::PushL(myExample); // push pointer to stack

 myExample->DoSomethingL()); // something that might leave

 // pop from cleanup stack, and destroy, in one operation
 CleanupStack::PopAndDestroy();
 }


Note