I know about memory management rules and there is no need of what I am asking if I follow these rules. But, I wonder if there is a way to know if an instance is already deallocated without throwing an exception.
My app uses an object what any view on my app can become its delegate. Sometimes, I’m getting this well known error. I can avoid this by setting the delegate to nil on the dealloc method of the current delegate owner.
In summary… Do I have any way to know if an object is deallocated?
Thanks.
Can I tell if a pointer now points to garbage?
No, not really. Once an object is deallocated, its memory can be reused at any time. Sometimes it’ll actually point to garbage (causing a crash), sometimes it’ll point to a different Obj-C object, and sometimes the memory will not have been reused yet.
The main exception is that if you set the environment variable
NSZombieEnabled=YES(in “edit scheme” somewhere in Xcode 4), the memory used by objects will never be deallocated (unless you also setNSDeallocateZombies=YES, or so); instead, sending a message to the zombie will cause an exception. It’s useful for debugging, largely because it tells you the class name of the instance that got deallocated.