开发者

EXC_BAD_ACCESS in main.m

Suddenly I got EXC_BAD_ACCESS on this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

Here is the code:

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool all开发者_开发问答oc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

I do not even know where to start looking?

Can anyone please help?


For any EXC_BAD_ACCESS errors, you are usually trying to send a message to a released object. The BEST way to track these down is use NSZombieEnabled.

This works by never actually releasing an object, but by wrapping it up as a "zombie" and setting a flag inside it that says it normally would have been released. This way, if you try to access it again, it still know what it was before you made the error, and with this little bit of information, you can usually backtrack to see what the issue was.

It especially helps in background threads when the Debugger sometimes craps out on any useful information.

VERY IMPORTANT TO NOTE however, is that you need to 100% make sure this is only in your debug code and not your distribution code. Because nothing is ever release, your app will leak and leak and leak. To remind me to do this, I put this log in my appdelegate:

if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
  NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");


EXC_BAD_ACCESS is a signal indicating you're trying to access a variable in memory that has been deallocated or doesn't exist. Since it's showing up after your autorelease pool is released, it means you probably over-released a variable with a pending autorelease, so the variable no longer existed to release when the autorelease pool was drained.

There are plenty of existing questions addressing this, the foremost of which is this one.


EXC_BAD_ACCESS often indicates that you've over-released memory. You can use the "Build And Analyze" command in Xcode to help track this down. I'd also suggest putting breakpoints and logging statements throughout your code to isolate the bug.


I had this problem and it might be because you're using Apple's latest main.m which does:

NSString * appDelegateClassName;

@autoreleasepool {
    // Setup code that might create autoreleased objects goes here.
    appDelegateClassName = NSStringFromClass([AppDelegate class]); }

return UIApplicationMain(argc, argv, nil, appDelegateClassName);

I don't use ARC and I changed it to the code they used to use:

@autoreleasepool
{
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

and that seems to work!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜