views:

42

answers:

1

Hi All

I am attempting to replace an object in an NSMutableArray. Elsewhere in my class I am doing it successfully. Below is an example of the code failure.

NSNumber* newObject = [NSNumber numberWithDoulble:myCalculation];
NSLog(@"Old object at 12:%@",[myMisbehavingArray objectAtIndex:12];
[myMisbehavingArray replaceObjectAtIndex:12 withObject:newObject];

I threw in the NSLog statement to make sure myMisbehavingArray was pointing at something and it is, but when I run the program I get a EXC_BAD_ACCESS at the point of the replaceObjectAtIndex method.

Any Ideas?

A: 

I wrote these blog entries to help people debug EXC_BAD_ACCESS

http://loufranco.com/blog/files/Understanding-EXC_BAD_ACCESS.html

http://loufranco.com/blog/files/debug-iphone-crash-EXC_BAD_ACCESS.html

When you do something innocuous that results in EXC_BAD_ACCESS, it's very likely that you have corrupted the heap before that. myMisbehavingArray might be pointing at an array, but there are many other pointers involved.

If you can reproduce the problem reliably in a very simple way, this line of code is like a heap corruption detector. You can move it around and see where it crashes and where it doesn't to try to narrow down the point where things went bad.

A harder, but more reliable way, is to Enable Guard Malloc and then use the debugger to turn on extra checking:

http://developer.apple.com/iphone/library/documentation/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html

Lou Franco