How to debug EXC_BAD_ACCESS in iPhone app when I can't determine the cause?

醉酒当歌 提交于 2019-12-03 04:17:59

you will want to enable zombie objects in your code, and Checking Autoreleased objects, and perhaps enabling debugging will help.

I added three Environment Variables.

  • NSZombieEnabled
  • NSAutoreleaseFreedObjectCheckEnabled
  • NSDebugEnabled

all of these are set to YES

here is a link with the path I took.

http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

if you are using XCode 4 then you will add these in the Arguments section of the Edit Schemes popover.

Another thing to note is, you should only release or autorelease objects that you are retaining. You maintain a retain on the following objects.

  • Any object you alloc [NSObject alloc]
  • Any object obtained using the static new command [NSObject new]
  • Any object you explicitly retain [myObject retain]
  • Any copy of an object [myObject copy]
  • Any property with the retain or copy attribute @property (retain) NSString *myProperty;

if you send an autorelease to any object other than these, you could randomly end up with this and other errors.

commonly I release objects and then set them to nil, that way if i release them later, I wont have any issue because if you autorelease nil, you get nil.

NSObject *myObject = [incomingObject retain];
// Do something with the object.
[myObject autorelease];
[myObject autorelease]; // This line will end in an error down the line when the object is released past 0, or when the release pool is drained.
myObject = nil;
[myObject release]; // This line will do nothing. no error, no effect.

The most likely cause is adding a garbage or already freed object to the Autorelease pool - maybe in that PurpleEventCallback function?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!