SlideShare a Scribd company logo
1 of 59
Symbian OS Memory Management v2.0a – 21 May 2008 1 Andreas Jakl, 2008
Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Symbian OS courses at the University of Applied Sciences in Hagenberg, Austria ( http://www.fh-hagenberg.at/ ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. Parts of these materials are based on information from Symbian Press-books published by John Wiley & Sons, Ltd. This document contains copyright materials which are proprietary to Symbian, UIQ, Nokia and SonyEricsson. “S60™” is a trademark of Nokia. “UIQ™” is a trademark of UIQ Technology. Pictures of mobile phones or applications are copyright their respective manufacturers / developers. “Symbian ™”, “Symbian OS ™” and all other Symbian-based marks and logos are trademarks of Symbian Software Limited and are used under license. © Symbian Software Limited 2006.  Andreas Jakl, 2008 2
Schedule Leaves, Panics and TRAP(D) Cleanupstack ObjectconstructionusingELeave Two-phaseconstruction Debugging tools Andreas Jakl, 2008 3
Leaves & Panics Handling problems Andreas Jakl, 2008 4
Exceptions – Java Try & Catch for handling exceptions Functions can throw “Exceptions” Andreas Jakl, 2008 5 Integer Class … static intparseIntthrowsNumberFormatException { … } … Calling function Try { int x = Integer.parseInt(“1234”); } catch (NumberFormatException e) { System.out.println(“Unable to convert this String to a number.”); }
Leave – Symbian TRAP(D) catches exceptions (“Leave”) Functions send out leave Function name marked by an L-suffix Andreas Jakl, 2008 6 The TRAP(D) macros are defined in e32cmn.h Main-Function TRAPD(err, DoExampleL());if (err)    {    console->Printf(KTxtFailed, err);    } DoExampleL()-Function voidDoExampleL()    {RFsfsSession;  // Connect to the file serverUser::LeaveIfError(fsSession.Connect()); // …fsSession.Close();    } TRAPD-Makro declares err as TInt and = KErrNone Leaves if the Connect() function does not return KErrNone
Central ExceptionHandling Andreas Jakl, 2008 7 New (ELeave) …  … NewL() …     … User::Leave() … … ConstructL() … F6L() F7L() F8L() F9L() F5L() F5L() …  … F6L() …. … F8L() …. F3L() F4L() F0L() F2L() … F3L() … F4L() … F1L() TRAPD(err, F2L()); if(err) …
Handling Leaves Try to implement central leave-handling If leave not handled by your code  error-message shown by the UI-framework! Therefore: Only handle leaves yourself if they influence your application Andreas Jakl, 2008 8
When can a function leave? Caused by your own code: User::Leave(), User::LeaveIfError(), User::LeaveNoMemory() or User::LeaveIfNull() Failed object-construction when using the “new (ELeave)”-operator Calling a function that potentially causes a leave e.g. x->DoSomethingL() Andreas Jakl, 2008 9
Details: Causing a Leave User::Leave(TIntaReason) Error code (aReason) = value that will be received by TRAP Causes leave in any case User::LeaveIfError(TIntaReason) Causes leave if parameter is negative, e.g.:TIntfoundAt = iElementArray.Find(example, aRelation);User::LeaveIfError(foundAt);	// Leaves if foundAt == KErrNotFound (-1) User::LeaveNoMemory() Is the same as: User:Leave(KErrNoMem); User::LeaveIfNull(TAny* aPtr) Andreas Jakl, 2008 10
TRAP / TRAPD Two trap harness macros: TRAP: declares the variable in which the leave code is returned TRAPD: declare a TIntvariable yourself If a leave occurs inside MayLeaveL(), which is executed inside the harness, the program code will return immediately to the TRAP harness macro The variable result will contain the error code associated with the leave or will be KErrNoneif no leave occured Andreas Jakl, 2008 11 TRAPD(result, MayLeaveL()); if (KErrNone!=result) ... is equivalent to: TIntresult; TRAP(result, MayLeaveL()); if (KErrNone!=result) ...
Tips Andreas Jakl, 2008 12 X voidInsertData()     { TInt err;     TRAP(err, iData->InsertL(23)); if (err) { ... }     TRAP(err, iData->InsertL(24)); if (err) { ... }     } TRAPs are expensive Don’t use several TRAPs right after each other Instead: Make the function leave (append an L to the function name) Handle all errors in a single TRAP-call one level above! In UI apps, many leaves don’t have to be handled by you  they can go up to the topmost level (= Active Scheduler) prefer  voidHandleCommand()     {     TRAPD(err, InsertDataL()); if (err) { ... }     } voidInsertDataL()     { iData->InsertL(23)); iData->InsertL(24));     }
Panics ... cannot be caught and handled! Terminates thread (= usually the whole application) Use them for checking code logic only Can also be sent out by the system for critical errors If a panic happens: Make sure you fix it, as you can’t handle it! Andreas Jakl, 2008 13 // Stray signal detected! _LIT(KMsgStraySignal, "Stray signal"); User::Panic(KMsgStraySignal, 1);		// Panic with code 1
Resource Handling Stack, Heap and the Cleanup Stack Andreas Jakl, 2008 14
Practice in a Nutshell All strategiesarepresent in thisfunction: Andreas Jakl, 2008 15 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }
Recap: Stack Andreas Jakl, 2008 16 // Stack (1) voidCTest::F1() { TInt x = 0; TInt y = 1; for(x = 0; x < 10; x++) 		y++ 	y = y + F2();  // Stack (4) } // Stack (5) TIntCTest::F2() { TInt a = 2; TInt b = 1; return a + b;   // Stack (3) } Stack (2) Stack (5) Stack (1) TInt y TInt x F1 Stack (2) TInt y TInt x F1 Stack (4) TInt b TInt a F2 TInt y TInt x F1 Stack (3)
Recap: Heap Use for: Objects that need a lot of memory ( stack-size is limited!) Required amount of memoryonlyknown at runtime Scope of heap-objects is not limited to a function Use pointers to pass to other functions/objects Andreas Jakl, 2008 17
Motivation Symbian OS designed to run for many years  No opportunity to reclaim leaked memory through regular restarts! Small memory leaks accumulate over time We only have a small memory to start with Therefore: Simply write perfect, leak-free code! Andreas Jakl, 2008 18
Motivation – How? By keeping track of all allocated objects! By making sure: All heap-objects are pointed to by at least one pointer, all the time – even in the constructor of an object! Free heap memory as soon as possible after use. Andreas Jakl, 2008 19
The Cleanup Stack Safeguard Andreas Jakl, 2008 20
Resource Handling – Rule 1 Andreas Jakl, 2008 21 Every local pointer to an instance of a heap-based object also has to be pushed on the cleanup stack, if there‘s the risk that the pointer gets lost because of a leave.
Cleanup Stack Situation: function creates local heap-object Before code gets to the delete-statement: error Function is left (Leave) Pointer-address on the stack is freed Object itself is orphaned memory leak! Andreas Jakl, 2008 22 voidCImage::DoSomethingDangerousL()     {     User::Leave(KErrNotFound);     } CImage voidCMyObj::DoSomethingL() { } CImage* img = new (ELeave) CImage(); img->DoSomethingDangerousL(); deleteimg; img = pointer on the stack to an instance on the heap
Cleanup Stack Memory situation if a leave occurs: Andreas Jakl, 2008 23 ! X X img Heap Stack Object stays on the heap;Pointer to delete the instance is lostmemory leak
Cleanup Stack Solution: Cleanup Stack Andreas Jakl, 2008 24 void CMyObj::DoSomethingL() { CImage* img = new (ELeave) CImage(); CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); } img  CleanupStack X X img Heap Stack When no leave occurs: object still has to be deleted by you + removed from the cleanup stack! Cleanup stack saves a second pointerto the object on the heap.
AdvancedCleanup Andreas Jakl, 2008 25 RFile Forresourcesthathavetobeclosed / freed(e.g.: files, sockets, …) Close(): CleanupClosePushL(T&) Release(): CleanupReleasePushL(T&) Destructor: CleanupDeletePushL(T*) Close() TCleanupOperation TAny* aPtr … TCleanupItem CleanupStack
AdvancedCleanup – Example Andreas Jakl, 2008 26 voidCMyClass::TransferDataL()     { RSocketServsocketServer;     // Connect to SocketServer     User::LeaveIfError( socketServer.Connect() );     // Make sure Close() is called at the end CleanupClosePushL( socketServer ); // … CleanupStack::PopAndDestroy();  //  socketServer     }
Cleanup Stack and Ownership Transfer It should never be possible for an object to be deleted more than once! Andreas Jakl, 2008 27 voidTransferOwnershipExampleL()     {     // The stack variable ptr points to memory allocated on the heap CItem* ptr = new (ELeave) CItem();     // The following function may leave -> place the pointer on the     // cleanup stack, so that the heap memory is freed in case      // AppendL() leaves. CleanupStack::PushL(ptr);     // iItemPtrArray takes ownership of the CItem object.     // This could fail, as it needs to allocate a new slot to store the pointer,      // therefore the object was placed on the cleanup stack in advance iItemPtrArray->AppendL(ptr);     // iItemArray now owns the heap object, so ptr may be safely popped off the stack.     // It shouldn’t be destroyed, as this would make the item in iItemPtrArray invalid! CleanupStack::Pop(ptr);     }
Practice in a Nutshell Strategiesexplaineduptonow: Andreas Jakl, 2008 28 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }  
Resource Handling – Rule 2 Andreas Jakl, 2008 29 Never push instance variables on the cleanup stack! The owning class is also on the cleanup stack somewhere (at least indirectly). This would lead to a double deletion of the object pointed to by the instance variable  Panic!
Coding Error Example Andreas Jakl, 2008 30 class CSimple : CBase     {        public:     ~CSimple();     void MayLeaveFuncL(); private:     void PrivateMayLeaveL(); CItem* iItem;     }; CSimple* simple = new (ELeave) CSimple(); CleanupStack::PushL(simple); 1. CSimple is created and pushed onto the cleanup stack as the next function may leave TRAPD(res,simple->MayLeaveFuncL()); ... 2. A leaving method is called on simple void CSimple::MayLeaveFuncL()     { iItem= new (ELeave) CItem(); CleanupStack::PushL(iItem); PrivateMayLeaveL(); CleanupStack::Pop(iItem);     } 3. The member variable is pushed onto the clean up stack (oops!) 5. The TRAP does the right thing and clears the cleanup stack; i.e. CSimple::iItem is deleted  4. What happens if a leaves occurs?  ... 6. The code logic completes with the popping and deleting of the simple object. CleanupStack::PopAndDestroy(simple); CSimple::~CSimple     {     delete iItem;     } BUT this calls the CSimple destructor, which deletes the iItem which has already been deleted by the TRAP   7. PANIC! Example taken from theSymbian Academy slides
Object Construction Leave-handling during Andreas Jakl, 2008 31
New Objects new-operator allocates memory and runs constructor Returns null-pointer if object creation fails(e.g. not enough memory)  manual test required to see if it was successful – no automated leave! Andreas Jakl, 2008 32 void CMyObj::DoSomethingL()     { CImage* img = newCImage();     if (img)          { CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); }      else         {         User::LeaveNoMemory();         }     }
New Objects – ELeave Andreas Jakl, 2008 33 void CMyObj::DoSomethingL()     { CImage* img = newCImage();     if (img)          { CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); }      else         {         User::LeaveNoMemory();         }     } void CMyObj::DoSomethingL()     { CImage* img = new (ELeave)CImage(); CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy();     } identical new-Operator overloaded with ELeave: automated leave if there’s not enough memory!
Practice in a Nutshell Strategiesexplained so far: Andreas Jakl, 2008 34 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }   
Two-Phase Construction The ultimate combination … Andreas Jakl, 2008 35
Resource Handling – Rule 3 Andreas Jakl, 2008 36 Neither a constructor nor a destructor may cause a leave! The destructor must not assume that the object was fully initialized.
Leaves in the Constructor Andreas Jakl, 2008 37 1. Call to constructorof CEngine Application CEngine* myEngine = new (ELeave) CEngine(); … … CEngine CEngine::CEngine() { iList = new (ELeave) CList(); } CEngine::~CEngine() { deleteiList; } iList CEngine 2. Leave – nomemory left for allocating iList Heap 3. Because of the leavethere is no valid pointerto the partially constructedCEngine-object. 4. Without a validpointer, the memoryalready allocatedfor CEngine is lost. ?
Solution: Two-phase Construction Andreas Jakl, 2008 38  Application CEngine* myEngine = new (ELeave) CEngine();CleanupStack::Push(myEngine); myEngine->ConstructL(); … // Note that the following line won’t be // reached in case of a leave!CleanupStack::PopAndDestroy(myEngine); CEngine CEngine::CEngine() {// 1. Phase } CEngine::ConstructL() {// 2. Phase iList = new (ELeave) CList(); } CEngine::~CEngine() { deleteiList; } iList CEngine Heap Object is fully allocated and on the cleanup stack  the destructor can be executed, the class is deleted by the cleanup stack  all memory is properly cleaned up in case of a leave!
Simplification: Two-phase Construction Less complicated creation of objects: Trailing C at the end of the function name: an object is left on the cleanup stack (as in NewLC()) Andreas Jakl, 2008 39 CEngine CEngine* CEngine::NewLC() {CEngine* self = new (ELeave) CEngine();    	// 1. PhaseCleanupStack::PushL(self);     self->ConstructL();    					// 2. Phase return self; } CEngine* CEngine::NewL() { CEngine* self = CEngine::NewLC(); CleanupStack::Pop(self); return self; } Application – Local Variable CEngine* myEngine = CEngine::NewLC(); … CleanupStack::PopAndDestroy(myEngine); or Application– Instance Variable iMyEngine = CEngine::NewL(); Destructor: delete iMyEngine; Application – Instance variable iMyEngine = CEngine::NewL(); // Destructor: deleteiMyEngine;
Practice in a Nutshell Strategiesexplained so far: Andreas Jakl, 2008 40  CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }     
Derivation Note: Some thoughts are necessary when deriving from a class that uses two-phase construction See the literature or Symbian Academy-slides for details! Andreas Jakl, 2008 41
Resource Handling – Rule 4 Andreas Jakl, 2008 42 If memory for a pointer (instance variable) is reallocated, set the old pointer to NULL beforehand.
NULL Situation (without NULL): AllocL() causes a leave, which propagates up … Instance of CElement is deleted Destructor of CElement is called iName still points to already deleted memory Deleted 2x           Panic Andreas Jakl, 2008 43 voidCEngine::DoStuffL(constTDesC& aName)     { CElement* element = CElement::NewLC();     TRAPD(err, element->SetNameL(aName)); CleanupStack::PopAndDestroy(element);         } 2 1 voidCElement::SetNameL(constTDesC& aName)     { deleteiName;	       // Deletes object, does not change pointer iName = aName.AllocL();	// Re-Allocation     } voidCElement::~CElement()     { deleteiName;     } 1 2 iName = NULL;       // Deletes pointer on stack Note: delete does not delete a NULL pointer.
Summary Andreas Jakl, 2008 44
Summary Catch leaves (= exceptions) with TRAP(D) Use cleanup stack for local heap-based variables Do not use the cleanup stack for instance variables No leaves in constructors or destructors Use two-phase construction for objects with data on the heap Set a pointer to NULL before re-allocating memory Andreas Jakl, 2008 45 !
What’s wrong? Andreas Jakl, 2008 46 CleanupStack::PushL(iList);
What’s wrong? Andreas Jakl, 2008 47 CleanupStack::PushL(iList); Never push instance variables on the cleanup stack! Twice as safe isn’t safe at all…
What’s wrong? Andreas Jakl, 2008 48 CEngine* engine = new (ELeave) CEngine();…CleanupStack::PopAndDestroy(engine)
What’s wrong? Andreas Jakl, 2008 49 CEngine* engine = new (ELeave) CEngine();…CleanupStack::PopAndDestroy(engine) enginewasn‘t pushed on the cleanup stack!new (ELeave) is only responsible for leaving if memory allocation for the object fails. new (ELeave) has nothing to do with the cleanup stack. Solution: Either: add the object to the cleanup stack: CleanupStack::PushL(engine); Or: don’t use the cleanup stack for deleting – in case no leaving operation is called between creation and deletion of the engine object: delete engine;
What’s wrong? Andreas Jakl, 2008 50 voidCGomokuViewGame::ChangeViewContextText(TIntaResourceId) 	{ RBufnewText(iEikonEnv->AllocReadResourceL(resourceId)); newText.CleanupClosePushL (); MQikViewContext* viewContext = ViewContext (); 	// Changing an alreadyexistingviewcontexttext viewContext->ChangeTextL (EGomokuViewContext, newText); CleanupStack::PopAndDestroy (1);	// newText 	}
What’s wrong? Andreas Jakl, 2008 51 void CGomokuViewGame::ChangeViewContextText(TInt aResourceId) 	{ 	RBuf newText(iEikonEnv->AllocReadResourceL(resourceId)); 	newText.CleanupClosePushL (); 	MQikViewContext* viewContext = ViewContext (); 	// Changing an already existing view context text 	viewContext->ChangeTextL (EGomokuViewContext, newText); 	CleanupStack::PopAndDestroy (1);	// newText 	} The trailing L of the function name is missing, as calls within the function can leave. Solution:void CGomokuViewGame::ChangeViewContextTextL(…)
Testing your Code On your way to perfection Andreas Jakl, 2008 52
Macros for Testing Wrap code you want to test within __UHEAP_MARK and __UHEAP_MARKEND: Andreas Jakl, 2008 53 CClass* p1 = new (ELeave) CClass; __UHEAP_MARK;// Mark start of test-area CClass* p2 = new (ELeave) CClass; CClass* p3 = new (ELeave) CClass; __UHEAP_CHECK(2);// 2 Objects (p2, p3) on the heap since the start-mark __UHEAP_CHECKALL(3);// In total 3 objects on the heap delete p3; _UHEAP_MARKEND;// Result: p2 is still here – Memory Leak! // or:  __UHEAP_MARKENDC(1);// Expects one cell on the heap
Finding Memory Leaks Emulator checksheapautomaticallyifyouexittheprogram BEFORE closingtheemulatorwindow! Andreas Jakl, 2008 54 Find it in theSymbian OS SDK doc: Symbian OS SDK v…  Symbian OS guide  Symbian OS reference  System panicreference
Memory Information Show numberofallocatedcells: Ctrl+Alt+Shift+A Andreas Jakl, 2008 55
Allocation Failure Intentional failing of memory allocation: __UHEAP_SETFAIL(aType, aValue); EDeterministic: fail every nth request ERandom: fail randomly once within a specified range – always using the same seed ETrueRandom: random seed taken from system time Andreas Jakl, 2008 56 __UHEAP_SETFAIL(RHeap::EDeterministic, 2); CObj* c1 = new (ELeave) CObj;		// will allocate CObj* c2 = new (ELeave) CObj;		// will fail CObj* c3 = new (ELeave) CObj;		// will allocate CObj* c4 = new (ELeave) CObj;		// will fail __UHEAP_RESET;	// Deactivate
FAILNEXT Fails thenextallocationrequest Andreas Jakl, 2008 57 CObj* c1 = new (ELeave) CObj;		// will allocate __UHEAP_FAILNEXT(1);// failnextallocation CObj* c2 = new (ELeave) CObj;		// will fail
AllocationFailure In theemulator, withoutwritingcode: Heap Failure Tool Activate:Ctrl+Alt+Shift+P Deactivate:Ctrl+Alt+Shift+Q Can failheap, WindowServer-allocationsorfile-access Andreas Jakl, 2008 58
… let’s move to the Challenges! Try it for your own Andreas Jakl, 2008 59

More Related Content

What's hot

Linux architecture
Linux architectureLinux architecture
Linux architecture
mcganesh
 

What's hot (20)

WEB INTERFACE DESIGN
WEB INTERFACE DESIGNWEB INTERFACE DESIGN
WEB INTERFACE DESIGN
 
Virtual machine security
Virtual machine securityVirtual machine security
Virtual machine security
 
Project control and process instrumentation
Project control and process instrumentationProject control and process instrumentation
Project control and process instrumentation
 
Virtualization
VirtualizationVirtualization
Virtualization
 
System dependability
System dependabilitySystem dependability
System dependability
 
Non-Uniform Memory Access ( NUMA)
Non-Uniform Memory Access ( NUMA)Non-Uniform Memory Access ( NUMA)
Non-Uniform Memory Access ( NUMA)
 
Difference Program vs Process vs Thread
Difference Program vs Process vs ThreadDifference Program vs Process vs Thread
Difference Program vs Process vs Thread
 
Memory management in linux
Memory management in linuxMemory management in linux
Memory management in linux
 
General purpose simulation System (GPSS)
General purpose simulation System (GPSS)General purpose simulation System (GPSS)
General purpose simulation System (GPSS)
 
4. system models
4. system models4. system models
4. system models
 
SPM PPT
SPM PPTSPM PPT
SPM PPT
 
Virtual machines and their architecture
Virtual machines and their architectureVirtual machines and their architecture
Virtual machines and their architecture
 
Operating System-Ch8 memory management
Operating System-Ch8 memory managementOperating System-Ch8 memory management
Operating System-Ch8 memory management
 
Open mp library functions and environment variables
Open mp library functions and environment variablesOpen mp library functions and environment variables
Open mp library functions and environment variables
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 
MG6088 SOFTWARE PROJECT MANAGEMENT
MG6088 SOFTWARE PROJECT MANAGEMENTMG6088 SOFTWARE PROJECT MANAGEMENT
MG6088 SOFTWARE PROJECT MANAGEMENT
 
Mobile hci
Mobile hciMobile hci
Mobile hci
 
Managing contracts
Managing contractsManaging contracts
Managing contracts
 
Selection of an appropriate project approach
Selection of an appropriate project approachSelection of an appropriate project approach
Selection of an appropriate project approach
 

Viewers also liked

Symbionic os
Symbionic osSymbionic os
Symbionic os
bhavithd
 
Chrome OS Observation
Chrome OS ObservationChrome OS Observation
Chrome OS Observation
Champ Yen
 

Viewers also liked (20)

Symbian OS
Symbian OSSymbian OS
Symbian OS
 
Symbian mobile operating system ppt
Symbian mobile operating system pptSymbian mobile operating system ppt
Symbian mobile operating system ppt
 
Symbian os presentation
Symbian os presentationSymbian os presentation
Symbian os presentation
 
Symbian Operating system
Symbian Operating systemSymbian Operating system
Symbian Operating system
 
Symbian os
Symbian osSymbian os
Symbian os
 
Symbian Os Introduction
Symbian Os IntroductionSymbian Os Introduction
Symbian Os Introduction
 
Symbian OS
Symbian  OS Symbian  OS
Symbian OS
 
Symbian OS
Symbian OSSymbian OS
Symbian OS
 
Part 2
Part 2Part 2
Part 2
 
Symbionic os
Symbionic osSymbionic os
Symbionic os
 
Communication in Symbian OS
Communication in Symbian OSCommunication in Symbian OS
Communication in Symbian OS
 
Symbian OS - Communication And Messaging
Symbian OS - Communication And MessagingSymbian OS - Communication And Messaging
Symbian OS - Communication And Messaging
 
Symbian OS Overview
Symbian OS OverviewSymbian OS Overview
Symbian OS Overview
 
Chrome OS Observation
Chrome OS ObservationChrome OS Observation
Chrome OS Observation
 
Google chrome os
Google chrome osGoogle chrome os
Google chrome os
 
Chrome OS: The Stateless Operating System
Chrome OS: The Stateless Operating SystemChrome OS: The Stateless Operating System
Chrome OS: The Stateless Operating System
 
Google chrome os
Google chrome osGoogle chrome os
Google chrome os
 
Google chrome
Google chromeGoogle chrome
Google chrome
 
Google Chrome OS
Google Chrome OSGoogle Chrome OS
Google Chrome OS
 
Chrome os
Chrome os Chrome os
Chrome os
 

Similar to Symbian OS - Memory Management

Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
Sri Prasanna
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Crash dump analysis - experience sharing
Crash dump analysis - experience sharingCrash dump analysis - experience sharing
Crash dump analysis - experience sharing
James Hsieh
 

Similar to Symbian OS - Memory Management (20)

Symbian OS - Active Objects
Symbian OS - Active ObjectsSymbian OS - Active Objects
Symbian OS - Active Objects
 
Symbian OS - Dynamic Arrays
Symbian OS - Dynamic ArraysSymbian OS - Dynamic Arrays
Symbian OS - Dynamic Arrays
 
Symbian OS - Types And Declarations
Symbian OS - Types And DeclarationsSymbian OS - Types And Declarations
Symbian OS - Types And Declarations
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
 
Symbian OS - Descriptors
Symbian OS - DescriptorsSymbian OS - Descriptors
Symbian OS - Descriptors
 
Computer Networks Omnet
Computer Networks OmnetComputer Networks Omnet
Computer Networks Omnet
 
Java ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and ThreadsJava ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and Threads
 
Java ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationJava ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and Localization
 
Lecture 12 os
Lecture 12 osLecture 12 os
Lecture 12 os
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
Symbian OS - Quick Start
Symbian OS - Quick StartSymbian OS - Quick Start
Symbian OS - Quick Start
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Java ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EJava ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics E
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Bugs from Outer Space | while42 SF #6
Bugs from Outer Space | while42 SF #6Bugs from Outer Space | while42 SF #6
Bugs from Outer Space | while42 SF #6
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
Crash dump analysis - experience sharing
Crash dump analysis - experience sharingCrash dump analysis - experience sharing
Crash dump analysis - experience sharing
 
Using Alf with Cameo Simulation Toolkit - Part 1: Basics
Using Alf with Cameo Simulation Toolkit - Part 1: BasicsUsing Alf with Cameo Simulation Toolkit - Part 1: Basics
Using Alf with Cameo Simulation Toolkit - Part 1: Basics
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and Eclipse
 

More from Andreas Jakl

More from Andreas Jakl (20)

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented Reality
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with Unity
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App Management
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSON
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web Technologies
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test Automation
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer Training
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC Quickstart
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App Scenarios
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC Development
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Symbian OS - Memory Management

  • 1. Symbian OS Memory Management v2.0a – 21 May 2008 1 Andreas Jakl, 2008
  • 2. Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Symbian OS courses at the University of Applied Sciences in Hagenberg, Austria ( http://www.fh-hagenberg.at/ ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. Parts of these materials are based on information from Symbian Press-books published by John Wiley & Sons, Ltd. This document contains copyright materials which are proprietary to Symbian, UIQ, Nokia and SonyEricsson. “S60™” is a trademark of Nokia. “UIQ™” is a trademark of UIQ Technology. Pictures of mobile phones or applications are copyright their respective manufacturers / developers. “Symbian ™”, “Symbian OS ™” and all other Symbian-based marks and logos are trademarks of Symbian Software Limited and are used under license. © Symbian Software Limited 2006. Andreas Jakl, 2008 2
  • 3. Schedule Leaves, Panics and TRAP(D) Cleanupstack ObjectconstructionusingELeave Two-phaseconstruction Debugging tools Andreas Jakl, 2008 3
  • 4. Leaves & Panics Handling problems Andreas Jakl, 2008 4
  • 5. Exceptions – Java Try & Catch for handling exceptions Functions can throw “Exceptions” Andreas Jakl, 2008 5 Integer Class … static intparseIntthrowsNumberFormatException { … } … Calling function Try { int x = Integer.parseInt(“1234”); } catch (NumberFormatException e) { System.out.println(“Unable to convert this String to a number.”); }
  • 6. Leave – Symbian TRAP(D) catches exceptions (“Leave”) Functions send out leave Function name marked by an L-suffix Andreas Jakl, 2008 6 The TRAP(D) macros are defined in e32cmn.h Main-Function TRAPD(err, DoExampleL());if (err) { console->Printf(KTxtFailed, err); } DoExampleL()-Function voidDoExampleL() {RFsfsSession; // Connect to the file serverUser::LeaveIfError(fsSession.Connect()); // …fsSession.Close(); } TRAPD-Makro declares err as TInt and = KErrNone Leaves if the Connect() function does not return KErrNone
  • 7. Central ExceptionHandling Andreas Jakl, 2008 7 New (ELeave) … … NewL() … … User::Leave() … … ConstructL() … F6L() F7L() F8L() F9L() F5L() F5L() … … F6L() …. … F8L() …. F3L() F4L() F0L() F2L() … F3L() … F4L() … F1L() TRAPD(err, F2L()); if(err) …
  • 8. Handling Leaves Try to implement central leave-handling If leave not handled by your code  error-message shown by the UI-framework! Therefore: Only handle leaves yourself if they influence your application Andreas Jakl, 2008 8
  • 9. When can a function leave? Caused by your own code: User::Leave(), User::LeaveIfError(), User::LeaveNoMemory() or User::LeaveIfNull() Failed object-construction when using the “new (ELeave)”-operator Calling a function that potentially causes a leave e.g. x->DoSomethingL() Andreas Jakl, 2008 9
  • 10. Details: Causing a Leave User::Leave(TIntaReason) Error code (aReason) = value that will be received by TRAP Causes leave in any case User::LeaveIfError(TIntaReason) Causes leave if parameter is negative, e.g.:TIntfoundAt = iElementArray.Find(example, aRelation);User::LeaveIfError(foundAt); // Leaves if foundAt == KErrNotFound (-1) User::LeaveNoMemory() Is the same as: User:Leave(KErrNoMem); User::LeaveIfNull(TAny* aPtr) Andreas Jakl, 2008 10
  • 11. TRAP / TRAPD Two trap harness macros: TRAP: declares the variable in which the leave code is returned TRAPD: declare a TIntvariable yourself If a leave occurs inside MayLeaveL(), which is executed inside the harness, the program code will return immediately to the TRAP harness macro The variable result will contain the error code associated with the leave or will be KErrNoneif no leave occured Andreas Jakl, 2008 11 TRAPD(result, MayLeaveL()); if (KErrNone!=result) ... is equivalent to: TIntresult; TRAP(result, MayLeaveL()); if (KErrNone!=result) ...
  • 12. Tips Andreas Jakl, 2008 12 X voidInsertData() { TInt err; TRAP(err, iData->InsertL(23)); if (err) { ... } TRAP(err, iData->InsertL(24)); if (err) { ... } } TRAPs are expensive Don’t use several TRAPs right after each other Instead: Make the function leave (append an L to the function name) Handle all errors in a single TRAP-call one level above! In UI apps, many leaves don’t have to be handled by you  they can go up to the topmost level (= Active Scheduler) prefer  voidHandleCommand() { TRAPD(err, InsertDataL()); if (err) { ... } } voidInsertDataL() { iData->InsertL(23)); iData->InsertL(24)); }
  • 13. Panics ... cannot be caught and handled! Terminates thread (= usually the whole application) Use them for checking code logic only Can also be sent out by the system for critical errors If a panic happens: Make sure you fix it, as you can’t handle it! Andreas Jakl, 2008 13 // Stray signal detected! _LIT(KMsgStraySignal, "Stray signal"); User::Panic(KMsgStraySignal, 1); // Panic with code 1
  • 14. Resource Handling Stack, Heap and the Cleanup Stack Andreas Jakl, 2008 14
  • 15. Practice in a Nutshell All strategiesarepresent in thisfunction: Andreas Jakl, 2008 15 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }
  • 16. Recap: Stack Andreas Jakl, 2008 16 // Stack (1) voidCTest::F1() { TInt x = 0; TInt y = 1; for(x = 0; x < 10; x++) y++ y = y + F2(); // Stack (4) } // Stack (5) TIntCTest::F2() { TInt a = 2; TInt b = 1; return a + b; // Stack (3) } Stack (2) Stack (5) Stack (1) TInt y TInt x F1 Stack (2) TInt y TInt x F1 Stack (4) TInt b TInt a F2 TInt y TInt x F1 Stack (3)
  • 17. Recap: Heap Use for: Objects that need a lot of memory ( stack-size is limited!) Required amount of memoryonlyknown at runtime Scope of heap-objects is not limited to a function Use pointers to pass to other functions/objects Andreas Jakl, 2008 17
  • 18. Motivation Symbian OS designed to run for many years  No opportunity to reclaim leaked memory through regular restarts! Small memory leaks accumulate over time We only have a small memory to start with Therefore: Simply write perfect, leak-free code! Andreas Jakl, 2008 18
  • 19. Motivation – How? By keeping track of all allocated objects! By making sure: All heap-objects are pointed to by at least one pointer, all the time – even in the constructor of an object! Free heap memory as soon as possible after use. Andreas Jakl, 2008 19
  • 20. The Cleanup Stack Safeguard Andreas Jakl, 2008 20
  • 21. Resource Handling – Rule 1 Andreas Jakl, 2008 21 Every local pointer to an instance of a heap-based object also has to be pushed on the cleanup stack, if there‘s the risk that the pointer gets lost because of a leave.
  • 22. Cleanup Stack Situation: function creates local heap-object Before code gets to the delete-statement: error Function is left (Leave) Pointer-address on the stack is freed Object itself is orphaned memory leak! Andreas Jakl, 2008 22 voidCImage::DoSomethingDangerousL() { User::Leave(KErrNotFound); } CImage voidCMyObj::DoSomethingL() { } CImage* img = new (ELeave) CImage(); img->DoSomethingDangerousL(); deleteimg; img = pointer on the stack to an instance on the heap
  • 23. Cleanup Stack Memory situation if a leave occurs: Andreas Jakl, 2008 23 ! X X img Heap Stack Object stays on the heap;Pointer to delete the instance is lostmemory leak
  • 24. Cleanup Stack Solution: Cleanup Stack Andreas Jakl, 2008 24 void CMyObj::DoSomethingL() { CImage* img = new (ELeave) CImage(); CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); } img  CleanupStack X X img Heap Stack When no leave occurs: object still has to be deleted by you + removed from the cleanup stack! Cleanup stack saves a second pointerto the object on the heap.
  • 25. AdvancedCleanup Andreas Jakl, 2008 25 RFile Forresourcesthathavetobeclosed / freed(e.g.: files, sockets, …) Close(): CleanupClosePushL(T&) Release(): CleanupReleasePushL(T&) Destructor: CleanupDeletePushL(T*) Close() TCleanupOperation TAny* aPtr … TCleanupItem CleanupStack
  • 26. AdvancedCleanup – Example Andreas Jakl, 2008 26 voidCMyClass::TransferDataL() { RSocketServsocketServer; // Connect to SocketServer User::LeaveIfError( socketServer.Connect() ); // Make sure Close() is called at the end CleanupClosePushL( socketServer ); // … CleanupStack::PopAndDestroy(); // socketServer }
  • 27. Cleanup Stack and Ownership Transfer It should never be possible for an object to be deleted more than once! Andreas Jakl, 2008 27 voidTransferOwnershipExampleL() { // The stack variable ptr points to memory allocated on the heap CItem* ptr = new (ELeave) CItem(); // The following function may leave -> place the pointer on the // cleanup stack, so that the heap memory is freed in case // AppendL() leaves. CleanupStack::PushL(ptr); // iItemPtrArray takes ownership of the CItem object. // This could fail, as it needs to allocate a new slot to store the pointer, // therefore the object was placed on the cleanup stack in advance iItemPtrArray->AppendL(ptr); // iItemArray now owns the heap object, so ptr may be safely popped off the stack. // It shouldn’t be destroyed, as this would make the item in iItemPtrArray invalid! CleanupStack::Pop(ptr); }
  • 28. Practice in a Nutshell Strategiesexplaineduptonow: Andreas Jakl, 2008 28 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }  
  • 29. Resource Handling – Rule 2 Andreas Jakl, 2008 29 Never push instance variables on the cleanup stack! The owning class is also on the cleanup stack somewhere (at least indirectly). This would lead to a double deletion of the object pointed to by the instance variable  Panic!
  • 30. Coding Error Example Andreas Jakl, 2008 30 class CSimple : CBase { public: ~CSimple(); void MayLeaveFuncL(); private: void PrivateMayLeaveL(); CItem* iItem; }; CSimple* simple = new (ELeave) CSimple(); CleanupStack::PushL(simple); 1. CSimple is created and pushed onto the cleanup stack as the next function may leave TRAPD(res,simple->MayLeaveFuncL()); ... 2. A leaving method is called on simple void CSimple::MayLeaveFuncL() { iItem= new (ELeave) CItem(); CleanupStack::PushL(iItem); PrivateMayLeaveL(); CleanupStack::Pop(iItem); } 3. The member variable is pushed onto the clean up stack (oops!) 5. The TRAP does the right thing and clears the cleanup stack; i.e. CSimple::iItem is deleted 4. What happens if a leaves occurs? ... 6. The code logic completes with the popping and deleting of the simple object. CleanupStack::PopAndDestroy(simple); CSimple::~CSimple { delete iItem; } BUT this calls the CSimple destructor, which deletes the iItem which has already been deleted by the TRAP 7. PANIC! Example taken from theSymbian Academy slides
  • 31. Object Construction Leave-handling during Andreas Jakl, 2008 31
  • 32. New Objects new-operator allocates memory and runs constructor Returns null-pointer if object creation fails(e.g. not enough memory)  manual test required to see if it was successful – no automated leave! Andreas Jakl, 2008 32 void CMyObj::DoSomethingL() { CImage* img = newCImage(); if (img) { CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); } else { User::LeaveNoMemory(); } }
  • 33. New Objects – ELeave Andreas Jakl, 2008 33 void CMyObj::DoSomethingL() { CImage* img = newCImage(); if (img) { CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); } else { User::LeaveNoMemory(); } } void CMyObj::DoSomethingL() { CImage* img = new (ELeave)CImage(); CleanupStack::PushL(img); img->DoSomethingDangerousL(); CleanupStack::PopAndDestroy(); } identical new-Operator overloaded with ELeave: automated leave if there’s not enough memory!
  • 34. Practice in a Nutshell Strategiesexplained so far: Andreas Jakl, 2008 34 CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }   
  • 35. Two-Phase Construction The ultimate combination … Andreas Jakl, 2008 35
  • 36. Resource Handling – Rule 3 Andreas Jakl, 2008 36 Neither a constructor nor a destructor may cause a leave! The destructor must not assume that the object was fully initialized.
  • 37. Leaves in the Constructor Andreas Jakl, 2008 37 1. Call to constructorof CEngine Application CEngine* myEngine = new (ELeave) CEngine(); … … CEngine CEngine::CEngine() { iList = new (ELeave) CList(); } CEngine::~CEngine() { deleteiList; } iList CEngine 2. Leave – nomemory left for allocating iList Heap 3. Because of the leavethere is no valid pointerto the partially constructedCEngine-object. 4. Without a validpointer, the memoryalready allocatedfor CEngine is lost. ?
  • 38. Solution: Two-phase Construction Andreas Jakl, 2008 38  Application CEngine* myEngine = new (ELeave) CEngine();CleanupStack::Push(myEngine); myEngine->ConstructL(); … // Note that the following line won’t be // reached in case of a leave!CleanupStack::PopAndDestroy(myEngine); CEngine CEngine::CEngine() {// 1. Phase } CEngine::ConstructL() {// 2. Phase iList = new (ELeave) CList(); } CEngine::~CEngine() { deleteiList; } iList CEngine Heap Object is fully allocated and on the cleanup stack  the destructor can be executed, the class is deleted by the cleanup stack  all memory is properly cleaned up in case of a leave!
  • 39. Simplification: Two-phase Construction Less complicated creation of objects: Trailing C at the end of the function name: an object is left on the cleanup stack (as in NewLC()) Andreas Jakl, 2008 39 CEngine CEngine* CEngine::NewLC() {CEngine* self = new (ELeave) CEngine(); // 1. PhaseCleanupStack::PushL(self); self->ConstructL(); // 2. Phase return self; } CEngine* CEngine::NewL() { CEngine* self = CEngine::NewLC(); CleanupStack::Pop(self); return self; } Application – Local Variable CEngine* myEngine = CEngine::NewLC(); … CleanupStack::PopAndDestroy(myEngine); or Application– Instance Variable iMyEngine = CEngine::NewL(); Destructor: delete iMyEngine; Application – Instance variable iMyEngine = CEngine::NewL(); // Destructor: deleteiMyEngine;
  • 40. Practice in a Nutshell Strategiesexplained so far: Andreas Jakl, 2008 40  CClass* CClass::NewL(TIntaInt, CBase& aObj) { CClass* self = new (ELeave) CClass(aInt); CleanupStack::PushL(self); self->ConstructL(aObj); CleanupStack::Pop(self); returnself; }     
  • 41. Derivation Note: Some thoughts are necessary when deriving from a class that uses two-phase construction See the literature or Symbian Academy-slides for details! Andreas Jakl, 2008 41
  • 42. Resource Handling – Rule 4 Andreas Jakl, 2008 42 If memory for a pointer (instance variable) is reallocated, set the old pointer to NULL beforehand.
  • 43. NULL Situation (without NULL): AllocL() causes a leave, which propagates up … Instance of CElement is deleted Destructor of CElement is called iName still points to already deleted memory Deleted 2x  Panic Andreas Jakl, 2008 43 voidCEngine::DoStuffL(constTDesC& aName) { CElement* element = CElement::NewLC(); TRAPD(err, element->SetNameL(aName)); CleanupStack::PopAndDestroy(element); } 2 1 voidCElement::SetNameL(constTDesC& aName) { deleteiName; // Deletes object, does not change pointer iName = aName.AllocL(); // Re-Allocation } voidCElement::~CElement() { deleteiName; } 1 2 iName = NULL; // Deletes pointer on stack Note: delete does not delete a NULL pointer.
  • 45. Summary Catch leaves (= exceptions) with TRAP(D) Use cleanup stack for local heap-based variables Do not use the cleanup stack for instance variables No leaves in constructors or destructors Use two-phase construction for objects with data on the heap Set a pointer to NULL before re-allocating memory Andreas Jakl, 2008 45 !
  • 46. What’s wrong? Andreas Jakl, 2008 46 CleanupStack::PushL(iList);
  • 47. What’s wrong? Andreas Jakl, 2008 47 CleanupStack::PushL(iList); Never push instance variables on the cleanup stack! Twice as safe isn’t safe at all…
  • 48. What’s wrong? Andreas Jakl, 2008 48 CEngine* engine = new (ELeave) CEngine();…CleanupStack::PopAndDestroy(engine)
  • 49. What’s wrong? Andreas Jakl, 2008 49 CEngine* engine = new (ELeave) CEngine();…CleanupStack::PopAndDestroy(engine) enginewasn‘t pushed on the cleanup stack!new (ELeave) is only responsible for leaving if memory allocation for the object fails. new (ELeave) has nothing to do with the cleanup stack. Solution: Either: add the object to the cleanup stack: CleanupStack::PushL(engine); Or: don’t use the cleanup stack for deleting – in case no leaving operation is called between creation and deletion of the engine object: delete engine;
  • 50. What’s wrong? Andreas Jakl, 2008 50 voidCGomokuViewGame::ChangeViewContextText(TIntaResourceId) { RBufnewText(iEikonEnv->AllocReadResourceL(resourceId)); newText.CleanupClosePushL (); MQikViewContext* viewContext = ViewContext (); // Changing an alreadyexistingviewcontexttext viewContext->ChangeTextL (EGomokuViewContext, newText); CleanupStack::PopAndDestroy (1); // newText }
  • 51. What’s wrong? Andreas Jakl, 2008 51 void CGomokuViewGame::ChangeViewContextText(TInt aResourceId) { RBuf newText(iEikonEnv->AllocReadResourceL(resourceId)); newText.CleanupClosePushL (); MQikViewContext* viewContext = ViewContext (); // Changing an already existing view context text viewContext->ChangeTextL (EGomokuViewContext, newText); CleanupStack::PopAndDestroy (1); // newText } The trailing L of the function name is missing, as calls within the function can leave. Solution:void CGomokuViewGame::ChangeViewContextTextL(…)
  • 52. Testing your Code On your way to perfection Andreas Jakl, 2008 52
  • 53. Macros for Testing Wrap code you want to test within __UHEAP_MARK and __UHEAP_MARKEND: Andreas Jakl, 2008 53 CClass* p1 = new (ELeave) CClass; __UHEAP_MARK;// Mark start of test-area CClass* p2 = new (ELeave) CClass; CClass* p3 = new (ELeave) CClass; __UHEAP_CHECK(2);// 2 Objects (p2, p3) on the heap since the start-mark __UHEAP_CHECKALL(3);// In total 3 objects on the heap delete p3; _UHEAP_MARKEND;// Result: p2 is still here – Memory Leak! // or: __UHEAP_MARKENDC(1);// Expects one cell on the heap
  • 54. Finding Memory Leaks Emulator checksheapautomaticallyifyouexittheprogram BEFORE closingtheemulatorwindow! Andreas Jakl, 2008 54 Find it in theSymbian OS SDK doc: Symbian OS SDK v…  Symbian OS guide  Symbian OS reference  System panicreference
  • 55. Memory Information Show numberofallocatedcells: Ctrl+Alt+Shift+A Andreas Jakl, 2008 55
  • 56. Allocation Failure Intentional failing of memory allocation: __UHEAP_SETFAIL(aType, aValue); EDeterministic: fail every nth request ERandom: fail randomly once within a specified range – always using the same seed ETrueRandom: random seed taken from system time Andreas Jakl, 2008 56 __UHEAP_SETFAIL(RHeap::EDeterministic, 2); CObj* c1 = new (ELeave) CObj; // will allocate CObj* c2 = new (ELeave) CObj; // will fail CObj* c3 = new (ELeave) CObj; // will allocate CObj* c4 = new (ELeave) CObj; // will fail __UHEAP_RESET; // Deactivate
  • 57. FAILNEXT Fails thenextallocationrequest Andreas Jakl, 2008 57 CObj* c1 = new (ELeave) CObj; // will allocate __UHEAP_FAILNEXT(1);// failnextallocation CObj* c2 = new (ELeave) CObj; // will fail
  • 58. AllocationFailure In theemulator, withoutwritingcode: Heap Failure Tool Activate:Ctrl+Alt+Shift+P Deactivate:Ctrl+Alt+Shift+Q Can failheap, WindowServer-allocationsorfile-access Andreas Jakl, 2008 58
  • 59. … let’s move to the Challenges! Try it for your own Andreas Jakl, 2008 59