You invoke undefined behavior by accessing free memory.
It can break, it can work fine, it can cause dancing unicorns to burst from your nose.
To detect memory errors during code development, you must enable NSZombie, see instructions here:
http://www.cocoadev.com/index.pl?NSZombieEnabled
Update
You may wonder why this works: of course, should the OS always throw an error when trying to access invalid memory?
The reason you don't always get the error (and why the behavior is undefined) is because the memory check is valid on every access, it will lead to performance degradation - i.e. the code will run slower, just to test for something that should never be.
Therefore, you must be careful to catch all these errors during development so that they never happen to the end user. NSZombies are the best tool to find them.
Another point - if you do βbuild and parseβ in xcode, it may also find this error during build. Of course, the static analyzer detects some memory errors during assembly.
source share