1

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!

1
  • This seems to be a bug in the Simulator 3.0 SDK. I don't see these error messages with SDK 3.1 of the Simulator, and I do not see the error on the iPhone.
    – mahboudz
    Sep 30, 2009 at 3:22

2 Answers 2

0

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: iPhone development: pointer being freed was not allocated

1
  • 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
    Sep 30, 2009 at 3:21
0

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).

1
  • This is for the iPhone, which does not have garbage collection. Sep 29, 2009 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.