Crash in drawRect - EXEC BAD ACCESS
My app crash in a custom UIView
drawRect.
Crash only occurs in low-memory situation. (After I run some games or apps, turning my app from background to foreground)
Before crash, it triggered several times drawRect, one of them, crash...
- (void)drawRect:(CGRect)rect {
NSLog(@" ## it is in drawRect");
//NSLog(@"timecardImage: %@ ",[timecardImage description]);
[timecardImage drawAtPoint:(CGPointMake(0.0f, 0.0f))];
}
Crash TB:
#0 0x31db6c9a in objc_msgSend ()
#1 0x000051c8 in -[Timecard drawRect:] (self=<value temporarily unavailable, due to optimizations>, _cmd=<value temporarily unavailable, due to optimizations>, rect={origin = {x = 0, y = 0}, size = {width = 75, height = 108}}) at /xcode_prj/TimesheetKeeper/Classes/Timecard.m:72
#2 0x34caba04 in -[UIView(CALayerDelegate) drawLayer:inContext:] ()
#3 0x37221fac in -[CALayer drawInContext:] ()
#4 0x37221d2a in backing_callback ()
#5 0x3722177c in CABackingStoreUpdate ()
#6 0x3722117e in -[CALayer _display] ()
#7 0x37220e8c in -[CALayer display] ()
#8 0x3721570c in CALayerDisplayIfNeeded ()
#9 0x372151cc in CA::Context::commit_transaction ()
#10 0x37214fd6 in CA::Transaction::commit ()
#11 0x37213906 in CA::Transaction::pop ()
#12 0x37213884 in +[CATransaction commi开发者_如何学Got] ()
#13 0x34df7bd2 in _UIWindowUpdateVisibleContextOrder ()
#14 0x34df7c66 in +[UIWindow _prepareWindowsForAppResume] ()
#15 0x34df03e4 in -[UIApplication _handleApplicationResumeEvent:] ()
...
Below is how I setup the UIView - self.timecard:
-(void)setupTimecard{
if (!self.timecard) {
self.timecard = [[Timecard alloc] init];
[self.timecard retain];
}
timecard.center = CGPointMake(self.view.center.x,10.0f);
[self.view insertSubview:timecard atIndex:2];
}
And,I comment the viewController's didReceiveMemoryWarning . In viewDidUnload, I even do nothing on "timecard" just release it in Dealloc.
This issue cost me several days and I still have no idea. Any ideas you guys provide, I will try. Thank you for your help.
## It's Done! ##
-(id)init {
UIImage *image = [UIImage imageNamed:@"timecard3.png"] ;
timecardImage = image;
[timecardImage retain]; // #1 this is the key.
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
self = [self initWithFrame:frame];
return self;
}
In drawRect, timecardImage is the receiver of drawAtPoint, which is just a pointer to an autorelease image. In memory warning issue, autolease image got released. And so did my timecardImage. After i add #1 line, just a retain, EXC BAD ACCESS never happens.
Thank you guys.
It's probable that timeCardImage
is being deallocated. If you are retain
ing it (which you should be doing), you should follow a release
with nil
ing timeCardImage
. This is assuming that you are release
ing the values in low memory situation.
精彩评论