tags:

views:

304

answers:

1

I am playing with a little Cocoa Apps I wrote where I add objects of a class Person to a NSMutableArray. When I try to access an object that is beyond the array limit all I get is an "beyond bounds" message on the console.

In fact I expected something worse. I expected the app to crash! But it doesn't.

I am not sure if this is a good or a bad thing but I wonder what does happen inside the apps to prevent it from crashing. I always had the idea I am accessing objects in memory who are not there. Normaly that would kill every application.

+4  A: 

In your case, you're not actually being allowed to access bad memory directly since you're interfacing through the NSArray object. Instead, what's happening is that you're giving the object a bad index, and it is informing you by throwing an exception.

By default, exceptions that happen on the main thread of an NSApplication application do not cause the application to terminate. Instead, they log the error and abort the current run loop cycle and then start the next cycle.

For more information on how you can deal with these exceptions during your debugging phase, please take a look at this document. It describes various techniques and responses you can use when dealing with application level exceptions.

Also, reading and writing bad memory doesn't always cause the application to crash, that's why those kinds of errors can be so hard to track down. Instead, you could simply be corrupting other objects and have no idea that you've done so until much latter in the application's cycle. The important thing to remember here is that NSApplication will squash most exceptions happening on the main thread, so you have to pay extra attention to that.

Jason Coco