在XCode中打破EXC_BAD_ACCESS? [英] Break on EXC_BAD_ACCESS in XCode?

查看:105
本文介绍了在XCode中打破EXC_BAD_ACCESS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发和XCode的新手,并不知道如何开始排除 EXC_BAD_ACCESS 信号。如何使XCode在导致错误的确切行中断裂?

I'm new to iPhone development and XCode in general and have no idea how to begin troubleshooting an EXC_BAD_ACCESS signal. How can I get XCode to break at the exact line that is causing the error?

我似乎无法获得XCode停止在线上导致问题,但我在调试控制台中看到以下行:

I can't seem to get XCode to stop on the line causing the problem, but I do see the following lines in my debug console:


Sun Oct 25 15:12 :14 jasonsmacbook
TestProject [1289]:
CGContextSetStrokeColorWithColor:
无效上下文

Sun Oct 25 15:12:14 jasonsmacbook TestProject[1289] : CGContextSetStrokeColorWithColor: invalid context

Sun Oct 25 15:12:14 jasonsmacbook
TestProject [1289]:
CGContextSetLineWidth:无效上下文

Sun Oct 25 15:12:14 jasonsmacbook TestProject[1289] : CGContextSetLineWidth: invalid context

Sun Oct 25 15:12:14 jasonsmacbook
TestProject [1289]:
CGContextAddPath:无效上下文

Sun Oct 25 15:12:14 jasonsmacbook TestProject[1289] : CGContextAddPath: invalid context

Sun Oct 25 15:12:14 jasonsmacbook
TestProject [1289]:
CGContextDrawPath:无效上下文

Sun Oct 25 15:12:14 jasonsmacbook TestProject[1289] : CGContextDrawPath: invalid context

2009-10-25 15:12:14.680
LanderTest [1289:207] *** - [CFArray
objectAtIndex:]:发送到$的消息b $ b取消分配的实例0x3c4e610

2009-10-25 15:12:14.680 LanderTest[1289:207] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x3c4e610

现在,我试图d原始到我从 UIGraphicsGetCurrentContext()中获取的上下文,并传递给我要绘制的对象。

Now, I am attempting to draw to the context I retrieve from UIGraphicsGetCurrentContext() and pass to the object that I want to draw with.

进一步的试用和错误调试,我发现一个 NSMutableArray 我有一个属性在我的班上是一个僵尸。我进入课程的 init 函数,这里是我使用的代码:

Further trial and error debugging and I found that an NSMutableArray I have a property for on my class was a zombie. I went into the init function for the class and here's the code I was using:

if ((self = [super init])) {
        NSMutableArray *array = [NSMutableArray array];
        self.terrainBlocks = array;
        [array release];
    }
    return self;    
}

我删除了 [array release] 行,它不再给我 EXC_BAD_ACCESS 信号,但我现在很困惑为什么这个工作。我以为当我使用该属性时,它会自动为我保留,因此我应该从 init 中释放它,以免我发生泄漏。我完全困惑了这个工作原理,所有的指南和Stackoverflow问题我只读了我更多的关于如何在我的init方法中设置属性。对于任何EXC_BAD_ACCESS错误,您通常会尝试发送,因此似乎没有一致意见。对于任何EXC_BAD_ACCESS错误,

I removed the [array release] line and it no longer gives me the EXC_BAD_ACCESS signal, but I'm now confused about why this works. I thought that when I used the property, it automatically retained it for me, and thus I should release it from within init so that I don't have a leak. I'm thoroughly confused about how this works and all the guides and Stackoverflow questions I've read only confuse me more about how to set properties within my init method. There seems to be no consensus as to which way is the best.

推荐答案

向已发布对象发送消息。使用 BEST 跟踪这些方法的方法是使用 NSZombieEnabled

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.

当Debugger有时候会抛出任何有用的信息时,它特别有助于后台线程。

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

非常重要的注意但是,您需要100%确保这仅在您的调试代码中,而不是您的分发代码。因为没有任何东西被释放,你的应用程序将泄漏和泄漏。要提醒我这样做,我把这个日志写在我的app_范例:

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 released, 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!");

如果您需要帮助查找确切的行,请执行构建和调试( CMD -Y )而不是Build-and-Run( CMD-R )。当应用程序崩溃时,调试器将会准确显示哪条线,并结合NSZombieEnabled,您应该能够准确找出原因。

If you need help finding the exact line, Do a Build-and-Debug (CMD-Y) instead of a Build-and-Run (CMD-R). When the app crashes, the debugger will show you exactly which line and in combination with NSZombieEnabled, you should be able to find out exactly why.

这篇关于在XCode中打破EXC_BAD_ACCESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆