boyfarrell.com

26 April 2007

malloc: *** Deallocation of a pointer not malloced: 0×500120; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug

I keep on getting this error all afternoon when a Cocoa application. It basically means ‘you are releasing something twice’. e.g.

WRONG!


... //lovely code ...
[object release];

... //lovely code making you forget things ...
[object release];

CORRECT!


... //lovely well behaved code ...
[object release];
... //lovely well behaved code not at all requiring a garbage collector.

I don’t know about how to use the apple developer tools to find this bug in the code, I just found it my trail and error. If you know how, let me know.

3 Comments currently posted.

Chinmoy says:

Hi Daniel,

You could use MallocDebug (see http://developer.apple.com/tools/performance/overview.html#mallocdebug) to find memory allocation related problems. MallocDebug is a standalone app that comes with the CHUD(Computer Hardware Understanding Development Tools) that you can download from Apple’s Developer Connection.

You could also add this trick after a call to release:

[object release]; //this is your first release
object = nil;

[object release]; //same as [nil release];
//nothing happens.
//actually something might happen, but
//not another release.

boyfarrell says:

Hi,

It never occurred to me to set the method to nil. That could be a good problem solving method. Cheers for the link.

Dan.

Chinmoy says:

Actually, I can’t take credit for setting the pointer to nil, because I read that in Aaron Hillegass’s book, Cocoa Programming for Mac OS X :)

Chinmoy.

Post a comment on this entry: