开发者

EXC_BAD_ACCESS from checking if statement conditions?

开发者 https://www.devze.com 2023-02-15 04:42 出处:网络
I\'m getting an EXC_BAD_ACCESS error message in my app. Based on a bunch of NSLog\'s I\'ve been able to determine that this is being caused while checking the conditions of an if statement. My if stat

I'm getting an EXC_BAD_ACCESS error message in my app. Based on a bunch of NSLog's I've been able to determine that this is being caused while checking the conditions of an if statement. My if statement is the following:

if ( ([todaysDate compare:mostRecentDate] != NSOrderedSame) || (mostRecentDate == nil) ) {
   //Do Stuff
}

Can someon开发者_Go百科e tell me why I might be getting this error?

NOTE: todaysDate and mostRecentDate are ivar's that get set a different points in the code


Your case is more likely to be caused by insufficient retains on your variables. This is especially common when you use an autoreleased object earlier without retaining the value so you don't own the object, it's deallocated earlier in the run loop.

EDIT: Since you are using an ivar, you could use @property (retain) to make sure your ivar is properly retained when you set it or use the retain keyword and handle the retain counts yourself.


EXC_BAD_ACCESS is caused when you send a message to an object that has been deallocated. Most likely at some point earlier in your code you release todaysDate or mostRecentDate without setting them to nil and thus are sending a message to a released object.


EXC_BAD_ACCESS is an operating-system-level exception saying that you're trying to access memory that doesn't belong to your application. This typically happens when an object has its ownership relinquished prematurely (sending release to early), or when you have not explicitly taken ownership of an object that you should have (using retain). There are a few things you can do in this situation:

  1. Run the static analyser, this will help catch cases where you have neglected the memory management rules.
  2. Enable zombies. This will tell you if you are trying to send an instance to a deallocated object.
0

精彩评论

暂无评论...
验证码 换一张
取 消