views:

79

answers:

2

Something must be wrong with this code right here:

+ (UIImage*)captureView:(UIView *)theView {
    UIGraphicsBeginImageContext(theView.frame.size);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

When I use that, Xcode throws me this error message:

malloc: * error for object 0x103f000: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

I can't see any memory management errors there. Does anyone else?

A: 

Don't see. Set NSZombie enabled in the build to track it down.

It might be related to returning an autoreleased UIImage from a class method which will probably be inside of a temporary autorelease pool. The image might be being destroyed by the draining of that pool. To test, move the method to an instance method and see if the problem goes away.

TechZen
NSZombie isnt helping much here, because the code in the stack trace is only arm instructions. Nothing from my own stuff visible there until the launch of main() function. Thanks for the hint with the class method. Did it as instance method, but it didn't solve the issue.
dontWatchMyProfile
NSZombie won't help here, as this appears to be happening 'below' the ObjC layer (malloc error).
Ben Gottlieb
I believe it's a framework bug in 3.0
dontWatchMyProfile
+1  A: 

I have had same warning, it does not occur in 3.1 or above.

EEE
probably a framework bug. It does only happen when I call -removeFromSuperview. When I get rid of it as part of my normal view controller drop process, which also removes the view, then all is fine.
dontWatchMyProfile