“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
To handle the exceptions in symbian we used the cleanup stack mechanism.The Symbian exception mechanism is based on leaving.The most important issue here is the Cleanup Stack.
Symbian OS is optimized for low resource usage, and when an unexpected event occurs, there is a need to clean up all the currently allocated objects. To ensure you actually de-allocate ANY object you create, there is a stack, named the “Cleanup Stack”, where you push any object that needs to be de-allocated later. Consider this example:
CDemo* demo = new CDemo;
DangerousOperationL();
delete demo;
If the DangerousOperationL() leaves, the delete demo will never be reached, hence leaving the demo object allocated. The correct way is to use the cleanup stack as following:
Ex.
CDemo* demo = new CDemo();
CleanupStack:: PushL(demo);
DangerousOperationL();
CleanupStack:: PopAndDestroy()
When you create an object with new or NewL, you have to push it on the Cleanup Stack if any action that follows can leave. If you allocate an object with NewLC, it will be already pushed on the Cleanup Stack at the creation.
After you are done with your object, you should destroy it. If the object is already on the Cleanup Stack, you should remove it from the stack with Pop then delete it. Or better, you can call PopAndDestroy to do both of them in a single step .
If a function leaves, control returns immediately to the trap harness within which it was called. This means that any automatic variables within functions called inside the trap harness are destroyed. A problem arises if any of these automatic variables are pointers to objects allocated on the heap: when the leave occurs and the pointer is destroyed, the object it pointed to is orphaned and a memory leak occurs.
Example:
TRAPD(error,doExampleL());
…
In this example, if myObject1 was new’ed successfully, but there was insufficient memory to allocate myObject2, myObject1 would be orphaned on the heap. Thus, we need some mechanism for retaining any such pointers, so that the memory they point to can be freed after a leave. Symbian OS mechanism for this is the cleanup stack.
ClenUp Stack
To handle the exceptions in symbian we used the cleanup stack mechanism.The Symbian exception mechanism is based on leaving.The most important issue here is the Cleanup Stack.
Symbian OS is optimized for low resource usage, and when an unexpected event occurs, there is a need to clean up all the currently allocated objects. To ensure you actually de-allocate ANY object you create, there is a stack, named the “Cleanup Stack”, where you push any object that needs to be de-allocated later. Consider this example:
CDemo* demo = new CDemo;
DangerousOperationL();
delete demo;
If the DangerousOperationL() leaves, the delete demo will never be reached, hence leaving the demo object allocated. The correct way is to use the cleanup stack as following:
Ex.
CDemo* demo = new CDemo();
CleanupStack:: PushL(demo);
DangerousOperationL();
CleanupStack:: PopAndDestroy()
When you create an object with new or NewL, you have to push it on the Cleanup Stack if any action that follows can leave. If you allocate an object with NewLC, it will be already pushed on the Cleanup Stack at the creation.
After you are done with your object, you should destroy it. If the object is already on the Cleanup Stack, you should remove it from the stack with Pop then delete it. Or better, you can call PopAndDestroy to do both of them in a single step .
If a function leaves, control returns immediately to the trap harness within which it was called. This means that any automatic variables within functions called inside the trap harness are destroyed. A problem arises if any of these automatic variables are pointers to objects allocated on the heap: when the leave occurs and the pointer is destroyed, the object it pointed to is orphaned and a memory leak occurs.
Example:
TRAPD(error,doExampleL());
…
void doExampleL()
{
CSomeObject* myObject1=new (ELeave) CSomeObject;
CSomeObject* myObject2=new (ELeave) CSomeObject; // WRONG
}
In this example, if myObject1 was new’ed successfully, but there was insufficient memory to allocate myObject2, myObject1 would be orphaned on the heap. Thus, we need some mechanism for retaining any such pointers, so that the memory they point to can be freed after a leave. Symbian OS mechanism for this is the cleanup stack.
Leave an Answer/Comment