Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

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



Cleanup requirements

When a function leaves, it transfers control directly to the statement following the TRAP (or TRAPD) macro under which it was invoked. This is carried out by setting the stack pointer to the context of the original TRAP macro, and jumping to the desired program location. Therefore,

This key aspect of EPOC exceptions has far-reaching implications:

The cleanup stack is the EPOC mechanism for handling this last problem.


Example

The problem for heap-allocated resources is shown below. If the call to DoSomethingL() leaves, the CExample object would be orphaned on the heap: the memory used for it could not have been recovered until the program terminates.

void doExampleL()
 {
 // An T-type object: can be declared on the stack
 TBuf<10> buf;

 // A C-type object: must be allocated on the heap
 // Allocate and leave if can not
 CExample* myExample = new (ELeave) CExample;

 // do something that cannot leave: no protection needed
 myExample->iInt = 5;

 // PROBLEM: do something that can leave
 myExample->DoSomethingL();
     // delete
 delete myExample;
 }