Snowcrash
Snowcrash

Reputation: 86157

Catching EXC_BAD_ACCESS

I wrote this code at the end of a long day:

MyObject *thisObj;
// ... lots of code here ...
thisObj.name = @"test"; // Which caused an EXC_BAD_ACCESS as I hadn't alloc / init'd thisObj.

It took me ages to figure out I'd forgotten to initialise the object so I was wondering - is there any way of catching this. I tried setting NSZombie but that didn't seem to do anything.

Any ideas on the best way to do this?

Upvotes: 0

Views: 651

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243156

Never declare a variable without initializing it to some value. Even

MyObject *thisObj = nil;

Is better than

MyObject *thisObj;

There's probably a compiler flag you can turn on to warn you about this.

EDIT:

Yep, you can use -wuninitialized -O (capital O, not 0) to get this:

enter image description here

Upvotes: 5

Related Questions