views:

1040

answers:

3

Hi,

I have pretty much finished my first working Symbian application, but in my hastened learning have paid little attention to memory management and pushing to and cleaning up the stack?

Could somebody please point me in the direction of some of the best practises to use here, and maybe some of the best leak detection/memory profiling tools.

For example, if I grab a TDesC or a TPtrC16 inside a function, how do I then clean them up the best way, is it simply

TPtrC16 temp = ...
temp.CleanupClosePushL();
CleanupStack::PopAndDestroy()

..for everything?

Thanks and please forgive me, I am a self confessed Symbian n00b.

+2  A: 

Things stored on the stack do not need to be stored on the cleanup stack (unless they need special handling (R Classes etc, see below) )

The cleanup stack is for deleting objects when a leave (think exception) occurs, which would otherwise leak memory.

The actual use of the cleanup stack is through the static functions CleanupStack::PushL(..) and CleanupStack::Pop / PopAndDestroy.

Some classes such as RFile,RFs have to closed rather than deleted, so for these functions have their ::Close function called so you should use the global function CleanupClosePushL(), which instead of calling the delete operator on your object on a leave, it calls the class' ::Close function instead.

To check your code for memory leaks, you can use the macros __UHEAP_MARK; and __UHEAP_MARKEND; which will verify that nothing is left on the heap from between these two calls.

If you leave anything on the cleanupstack in an CActive's::RunL, the active scheduler will panic.

As a general technique, if a function that you are calling could leave, (denoted by a trailing 'L') then anythign that must be deleted or closed (etc) should be added to the cleanup stack.

Dynite
+1  A: 

It takes a little under 10 pages to properly exmplain all you need to know about the CleanupStack and Memory management for Symbian C++.

Shameless plug : this book was written specifically for your level of n00bness: http://www.quickrecipesonsymbianos.com

QuickRecipesOnSymbianOS
+3  A: 

I have in the past used HookLogger from Symbian to trace and investigate memory leaks. It is not the best, but it sure does help. Also, the heap markers raise ALLOC panics in case of memory leaks whenever your exit your application. The information those panics provide is barely helpful, but it is good indication that there is a memory leak somewhere. From there, you may use tools like HookLogger to narrow in on which part of the code the leak is getting introduced at.

I mentioned a few Symbian resources in reply to this thread. While all those have been most useful for me, I did find the book Accredited Symbian Developer Primer immensely helpful. If you could get your hands on it, I would definitely recommend it. It is a quick read, but it explains the concepts brilliantly, including things about memory management and descriptors.

ayaz