Wednesday, 21 September 2011

Error "EXC_BAD_ACCESS" fixed by adding "retain" to NSString

Was working on my next project and for some unknown reason, during the main loop it will work perfectly on 1st round, but then always crashed with this strange "EXC_BAD_ACCESS" error which I never seen before. Sometimes it shows this screen full of assembly languages as shown below.

Sometimes it's like this:

Tried lots of different things, like restructuring the classes, moving methods up and down the hierarchy, add lots of NSLog and checking object allocation/deallocation, check for memory leak in Instruments...etc.

Search on the net (see this one for example) and learned that the error is more about trying to call method of a object that's already deleted, not the memory leak. Have to use "Enable NSZombie detection" as shown below to check.

And I got it straight away!

After a while finally worked out that I was incorrectly doing something like this;

recordOfComputersMove = [recordOfComputersMove stringByAppendingFormat:@"%@%i",kStringSeparater,curEmptySpot];


which should actually be like this, adding the "retain" at the back fixed the problem.

recordOfComputersMove = [[recordOfComputersMove stringByAppendingFormat:@"%@%i",kStringSeparater,curEmptySpot] retain];



while working on the issue, I also read a few articles talking about this thing called "Enable Guard Malloc" (see here and here), and it took me a while to find out how to do it in XCode 4 (see here) as shown below inside "Edit Scheme".

However I think I don't understand how to use it "properly" yet as after it's been enabled, all I can see is this message as shown below, saying "stack log" been created in this file. But when I tried to open that file, it's in some sort of special format which I can't find a proper reader/editor to interpret it into a readable way.

Well, I guess most important thing for me is to identify the problem and work out a fix. That "Guard Malloc" way is definitely a bit too advanced for my level I guess...

Yeah, feels good to learn new things every day!

No comments:

Post a Comment