macos 为什么我的可可程序在启动时得到 EXC_BAD_ACCESS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:53:27  来源:igfitidea点击:

Why is my cocoa program getting EXC_BAD_ACCESS during startup?

macoscocoaexc-bad-access

提问by AlanKley

During the load of my cocoa application, my program crashes with the messsage EXC_BAD_ACCESS. The stack trace is not helpful. Any clues to how I can find the problem?

在加载我的可可应用程序期间,我的程序崩溃并显示消息 EXC_BAD_ACCESS。堆栈跟踪没有帮助。关于如何找到问题的任何线索?

回答by crackity_jones

I've seen times where this can happen when you are trying to access a object that you didn't retain properly so its either not pointing to a valid copy of your object or its pointing to an object of another type. Placing breakpoints early and analyzing the objects as you step through startup using po and print in gdb is your best bet.

我见过很多次,当您尝试访问未正确保留的对象时,可能会发生这种情况,因此它要么未指向对象的有效副本,要么指向另一种类型的对象。尽早放置断点并在您逐步启动时使用 gdb 中的 po 和 print 分析对象是您最好的选择。

回答by mmalc

This is typically indicative of a memory management error.

这通常表示内存管理错误。

Make sure all your outlet declarations follow best practice:

确保您的所有出口声明都遵循最佳实践:

@interface MyClass : MySuperclass {
    UIClass *myOutlet;
}
@property (nonatomic, retain) IBOutlet UIClass *myOutlet;
@end

This format ensures that you get memory management right on any platform with any superclass.

这种格式可确保您在具有任何超类的任何平台上获得正确的内存管理。

Check any awakeFromNibmethods to ensure that you're not over-releasing objects etc.

检查任何awakeFromNib方法以确保您没有过度释放对象等。

回答by Jonathan Moffatt

A new answer to an old thread... in XCode 4 the most effective way to diagnose EXC_BAD_ACCESS exceptions is to use Instruments to profile your app (from XCode click Product/Profile and choose Zombies). This will help you identify messages sent to deallocated objects.

旧线程的新答案...在 XCode 4 中,诊断 EXC_BAD_ACCESS 异常的最有效方法是使用 Instruments 来分析您的应用程序(从 XCode 单击 Product/Profile 并选择 Zombies)。这将帮助您识别发送到释放对象的消息。

回答by millenomi

To add: the foremost reason for unarchiving failure is forgetting "return self;" from the -init of a custom class. It hurts a lot :(

补充一点:取消归档失败的首要原因是忘记“返回自我”;来自自定义类的 -init。非常疼 :(

回答by millenomi

Check console log ( Applications/Utilities/Console.app ) . When program crashes on startup, and there's something wrong with initialization, it often writes out some helpful error messages there, before it crashes.

检查控制台日志( Applications/Utilities/Console.app )。当程序在启动时崩溃,并且初始化出现问题时,它通常会在崩溃之前写出一些有用的错误消息。

回答by AlanKley

This is one possible reason. There is a IBOutlet object that isn't being initialized and a message is being invoked on nil. The stack trace might look like this:

这是一种可能的原因。有一个未初始化的 IBOutlet 对象,并且在 nil 上调用了一条消息。堆栈跟踪可能如下所示:

#0    0x90a594c7 in objc_msgSend
#1    0xbffff7b8 in ??
#2    0x932899d8 in loadNib
#3    0x932893d9 in +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
#4    0x9328903a in +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:]
#5    0x93288f7c in +[NSBundle(NSNibLoading) loadNibNamed:owner:]
#6    0x93288cc3 in NSApplicationMain
#7    0x00009f80 in main at main.mm:17

Since the stack trace is not helpful you will have to step through your code to find the error. If for some reason you aren't able to set breakpoints early in your execution, try inserting some Debugger(); calls which will break to the debugger.

由于堆栈跟踪没有帮助,您将不得不单步执行代码以查找错误。如果由于某种原因您无法在执行的早期设置断点,请尝试插入一些 Debugger(); 将中断调试器的调用。