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

后端 未结 2 1382
故里飘歌
故里飘歌 2021-02-06 07:57

Help, I\'ve been hacking at this for weeks now! I have this app I am developing in the Simulator, and I have done a lot of the UI stuff, it is just getting to the interesting pa

相关标签:
2条回答
  • 2021-02-06 08:26

    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.
    
    0 讨论(0)
  • 2021-02-06 08:40

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

    0 讨论(0)
提交回复
热议问题