I have this code in one of my NSManagedObjects:
if (self.tempImageStorage) {
return self.tempImageStorage;
} else if(self.imageData) {
NSLog(@"%@ %d",self.imageData, self.imageData.length);
self.tempImageStorage = [UIImage imageWithData:self.imageData];
return self.tempImageStorage;
}
Occasionally, usually when I’m flipping through the images quickly, it will come back with an EXC_BAD_ACCESS on the 5th line (UIImage imageWithData line), and I can’t po (printobject) self in the console, so I assume that the NSMananagedObject itself has been deallocated. What doesn’t make sense is that it was able to reference self for the 2 ifs, and I even log the image data and its length just fine right before it.
How could the NSManagedObject be deallocated in during that period since this is all happening on the same thread (and it’s the main thread)?
PS: I have Zombies enabled but it still doesn’t let me po self.
Code that sets the image data:
- (void) processRequest:(ASIHTTPRequest *) request
{
UIImage * orig = [UIImage imageWithData:[request responseData]];
CGSize targetSize = isPad()?CGSizeMake(446,150):CGSizeMake(320, 108);
CGRect fourPanelSize = CGRectMake(0, 0, 1000, 336);
CGImageRef imageRef = CGImageCreateWithImageInRect([orig CGImage], fourPanelSize);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone || bitmapInfo == kCGImageAlphaLast) {
bitmapInfo = kCGImageAlphaPremultipliedLast;
}
CGContextRef bitmap;
bitmap = CGBitmapContextCreate(NULL, targetSize.width, targetSize.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
CGContextDrawImage(bitmap, CGRectMake(1, 1, targetSize.width-2, targetSize.height-2), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
NSData * thumbData = UIImagePNGRepresentation(newImage);
NSData * imageData = UIImagePNGRepresentation(orig);
//Post notification on main thread since it will be updating the UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.tempImageThumbStorage = newImage;
self.imageThumbnailData = thumbData;
self.tempImageStorage = orig;
self.imageData = imageData;
[(AppDelegate*)[UIApplication sharedApplication].delegate saveContext];
NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] initWithCapacity:1];
[userInfo setObject:[NSNumber numberWithBool:YES] forKey:@"status"];
[userInfo setObject:self forKey:@"comic"];
[[NSNotificationCenter defaultCenter] postNotificationName:PCRLoadMediahNotification object:self userInfo:userInfo];
strongSelf = nil;
}];
}
Turns out I was saving the context too often which was causing the imageData not to be saved to disk properly, and causing the exception.