EXC_BAD_ACCESS in drawRect
The code below "sometimes" causes a crash (EXC_BAD_ACCESS) when run on the device. Never on the simulator.
To reproduce it I keep 开发者_JAVA技巧overlaying a modal view controller over my table view controller. It usually happens when the modal view controller is dismissed.
Any ideas why this happens?
CGContextRef context = UIGraphicsGetCurrentContext();
//set the background of the cell
[self.backgroundColor set];
CGContextFillRect(context, rect);
// get cached image
UIImage *image = [[ImageUtil sharedInstance] getImageByRouteType:route.type];
CGSize imageSize = CGSizeMake(IMAGE_WIDTH, IMAGE_WIDTH);
// DEBUGGER STOPS ON THIS NEXT LINE, image object is fine though
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[...]
Thanks
If you use drawInRect in a multiple tread situation like NSOperationQueue, try use a lock to avoid "drawInRect" is called in more than one thread. I met a similar issue and solved it in this way.
@synchronized([UIImage class]){
UIGraphicsBeginImageContext(newSize);
CGRect rect = CGRectMake(0.0, 0.0, newSize.width, newSize.height);
[self drawInRect: rect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
精彩评论