Block Overruns

The Palm heap is stored as a linked list of headers. Between those headers are chunks, either allocated or free. When you allocate a chunk, the memory manager finds a free chunk. If it is exactly the right size, it’s returned. If not, the memory manager breaks it into two pieces — your chunk, and the remaining free chunk. Each of these chunks has a header. When you release a chunk, the memory manger checks the chunks before and after. If they’re free, the memory manager merges them together and one of the headers is abandoned.

Thus, there will always be a header before and after your block. If you overwrite your block, you will stomp on one of the control headers and have a corrupt heap. A corrupt heap causes the memory manager to do unexpected things, and you’re pretty much doomed at that point.

The Palm debug ROM is supposed to guard against this and show an error when it occurs, but in some cases this seems to fail. Learn to recognize the symptoms — mostly, deep recursion and other weird behaviour deep in the memory manager — and you’ll be better off. And consider checking the heap in the debug target of your application before and after you use a complicated function to write to memory. I plan on adding some to my application tomorrow morning.

Leave a Reply