Cleanup stack

From Wikipedia, the free encyclopedia

Cleanup Stack is a concept widely used in Symbian OS. It is most suitable to use in places where dynamic memory is used (allocated) in programming. The problem with dynamic memory is the sole discretion of the underlying OS whether the request for memory allocation shall succeed or not. Applications (Requester of memory) must be prepared to handle the rejection. In large programs dynamic memory is used almost everywhere. If an application frequently adds the code to handle this failure then it will increase the code size significantly. Symbian is used mostly on phones where this increase in the code size will further amplify the memory allocation failures. Symbian features an ingenious solution to that problem. When an application notes a memory allocation may fail, it places the earlier allocated memory address to a location which Symbian is aware of. That location is called Cleanup Stack. In the event of failure, Symbian knows that whatever resource is placed on the Cleanup Stack needs be freed. This way all the resources are freed when a program crashes (or Leaves). This freeing is performed automatically by the Symbian OS. Applications die peacefully without worrying who would clean up the mess left after them. Cleanup stack make an idea to keep a copy of pointer to allocated memory and all elements from the cleanup stack are popped out and destroyed by using Push(), Pop(), and PopAndDestroy(). For example,

 CleanupStack::PushL(ptr)
 CleanupStack::Pop()
 CleanupStack::PopAndDestroy()