views:

248

answers:

2

I am tracking down a strange bug. The bug manifests itself with the display of this message in the console log:

BlurApp(5018,0xa00d6500) malloc: *** error for object 0x103b000: pointer being freed was not allocated

This message pops in after execution has left my event loop. It seems to be happening when the autorelease pool is being drained. Here is the code that seems to trigger the error:

- (UIImage*)imageWithImage:(UIImage*)image 
     scaledToSize:(CGSize)newSize;
{
 UIGraphicsBeginImageContext( newSize );
 [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return newImage;
}

I call imageWithImage this way:

UIImage* theImage = [self imageWithImage:origImage scaledToSize:rect.size];

theImage is used in the calling routine and then I assume autorelease will take care of it.

If I change the return newImage; to return [newImage retain]; the error message is not seen.

Now, here is what is very strange. This error only occurs with certain sizes of newSize. specifically, it always happens when newSize's height and width are equal to any number in the range of 55 to 176 excluding 56, 110, 112, 114, and not for other values of height and width between 5 and 300. (height and width are always equal for all tests)

I could use some fresh eyes on this... thanks!

A: 

theImage is used in the calling routine and then I assume autorelease will take care of it.

If I change the return newImage; to return [newImage retain]; the error message is not seen.

Now, here is what is very strange. This error only occurs with certain sizes of newSize.

In a garbage collected language, Objects may be cleaned up (freed) by the garbage collector anytime there are no longer references to the object. In order to improve performance, garbage collection may be delayed -- i.e. until there is either idle time, a certain amount of time has passed, or until memory conditions reach a certain threshold.

As a result, some objects may be freed immediately, others may freed much later or never freed (until the environment shuts down).

Adisak
This is for the iPhone, which does not have garbage collection.
Dave DeLong
A: 

Try printing the retainCount of the image returned from UIGraphicsGetImageFromCurrentImageContext.

Something like: NSLog( @"Image retain count: %u", [newImage retainCount] );

or put a breakpoint on the code and type the following at the gdb console:

print (int) [newImage retainCount]

The retainCount should be greater than zero, probably one. Step through the code and keep checking the retainCount to see when it hits zero.

There's a similar thread at: http://stackoverflow.com/questions/1424210/iphone-development-pointer-being-freed-was-not-allocated

EricS
The link at the bottom points to a similar issue which makes me think this is a bug in the Simulator. I should have mentioned I was running this on the Simulator. I tried the app on the iPhone. No errors there. I also tried with Simulator 3.1 SDK instead of 3.0. No errors under Simulator 3.1.
mahboudz