views:

2914

answers:

5

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?

A: 

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:

#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.

AlanKley
Messages to nil are not an error in Objective-C.
mmalc
A: 

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

millenomi
+2  A: 

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.

crackity_jones
A: 

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.

+2  A: 

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.

mmalc