How to debug memory allocation?
How can I debug memory allocation code?
The following discussion focuses on the CodeWarrior debug console, but it also applies to Palm's PalmDebugger?, which comes with the SDK. These tools are a little cryptic, but it's pretty simple to debug memory problems once you know what everything means.
Lets say you debug when your program exits and you see the memory leak dialog.
Now, open the console and say "hd 0".
Look through the list for memory that belongs to your program. These chunks will usually have owner #2 and a star at the front of the line. Note that the CW debugger on Windows reverses the labels for lock and owner the first number shown is the owner.
Say you have something that looks like this:
start handle localID size size lck own flags
-------------------------------------------------------
*000046D4 00001B9C 00001B9D 00000C 000014 #2 #1 fM
The first thing you can tell is that the block of memory starts at 0x46D4. So, if you do "Data/View Memory" from CW, you can enter that hex address and see the contents of the lost chunk.
The second thing to notice is the size. Here, the chunk is 0xC bytes == 12 bytes. You can use that along with the contents to try to guess where this memory was allocated by your program.
Here, the lock count is 1 that means the chunk was allocated with MemHandleNew() and is locked. If its #15, it was allocated by MemPtrNew().
Finally, the handle is 0x1B9C that means the MemHandle variable that goes with this would have the value 0x80001B9C.
This last bit is useful for debugging code in the middle of a program. If you break somewhere and just have a handle that isn't locked, you can say "hd 0", find the handle, then use that to get the address of the chunk that goes with the handle for viewing in the memory window. It would be nice to automate this, but that's beyond the CW debugger for the time being.