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 had开发者_Python百科n'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?
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:
精彩评论