I am trying a simple test for a much more complex project but I am baffled as to why the code below is crashing and giving an EXC_BAD_ACCESS error?
This is called from a UIView.
- (void)testing {
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ball.png" ofType:nil];
CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
// CGImageRetain(imageRef);
UIImage *newImage = [[UIImage alloc]initWithCGImage:imageRef];
UIImageView *iv = [[UIImageView alloc]initWithImage:newImage];
[self addSubview:iv];
}
My guess is that the CGImageRef is not being retained but adding CGImageRetain(imageRef); makes no difference.
I should also note that this project has ARC turned on.
EDIT
I did a little bit more testing and have discovered that this is directly related to ARC as I created 2 basic projects including only the code above. The first with ARC turned off and it worked perfectly. The next with ARC turned on and BAM crash with the same error. The interesting thing is that I got an actual log error ONLY the first time I ran the project before the crash.
Error: ImageIO: ImageProviderCopyImageBlockSetCallback 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...
This line is the problem:
The created
UIImagewill be released immediately following this full-expression (e.g. after this line). So even trying to add aCGImageRetain()afterwards won’t work.The fundamental problem is the
CGImageRefreturned fromCGImageis almost certainly an ivar of theUIImageand will be released when theUIImageis deallocted.The generic way to fix this is to extend the lifetime of the
UIImage. You can do this by placing theUIImageinto a local variable and referencing it after your last reference to theCGImage(e.g. with(void)uiimageVar). Alternatively, you can retain theCGImageRefon that same line, as inBut if you do this, don’t forget to release the
imageRefwhen you’re done.