为什么我的cocoa程序在启动时会获得EXC_BAD_ACCESS?

时间:2022-09-27 23:20:27

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?

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

6 个解决方案

#1


5  

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.

我曾经看到过,当你试图访问一个你没有正确保留的对象时会发生这种情况,因此它不是指向对象的有效副本,也不是指向另一种类型的对象。早期放置断点并在使用po并在gdb中打印启动时分析对象是最好的选择。

#2


2  

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 awakeFromNib methods to ensure that you're not over-releasing objects etc.

检查所有awakeFromNib方法,以确保您不会过度释放对象等。

#3


1  

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单击产品/配置文件并选择Zombies)。这将帮助您识别发送到解除分配对象的消息。

#4


0  

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

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

#5


0  

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)。当程序在启动时崩溃,并且初始化出现问题时,它会在崩溃之前在那里写出一些有用的错误消息。

#6


-3  

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();将打破调试器的调用。

#1


5  

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.

我曾经看到过,当你试图访问一个你没有正确保留的对象时会发生这种情况,因此它不是指向对象的有效副本,也不是指向另一种类型的对象。早期放置断点并在使用po并在gdb中打印启动时分析对象是最好的选择。

#2


2  

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 awakeFromNib methods to ensure that you're not over-releasing objects etc.

检查所有awakeFromNib方法,以确保您不会过度释放对象等。

#3


1  

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单击产品/配置文件并选择Zombies)。这将帮助您识别发送到解除分配对象的消息。

#4


0  

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

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

#5


0  

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)。当程序在启动时崩溃,并且初始化出现问题时,它会在崩溃之前在那里写出一些有用的错误消息。

#6


-3  

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();将打破调试器的调用。