I have a pretty basic question. In some examples I’ve seen, objects are just released in the dealloc method. In others, the objects are released and then set to nil. Is there a reason for this? Is setting to nil after releasing advantageous?
I have a pretty basic question. In some examples I’ve seen, objects are just
Share
Three ways to dealloc
1. Just release
Now the object reference points to a random position, which may be one of two things:
The effect of a further method calls through this pointer is one of these three (which one is undefined):
EXC_BAD_ACCESSbecause the pointer points to garbage.2. Release and nil
Now the object reference is nil and any further method calls are ignored. This may silently cause a defined but unforeseen lateral effect in your code, but at least it doesn’t crash your application.
3. Nil and release
This is the same as before, but it removes that small window between release and nil where the object reference points to an invalid object.
Which one is best?
It is a matter of choice:
NSZombieEnabled=TRUEthen just release, don’t nil the zombie!Macros and zombies
A easy way to defer your choice is using a macro. Instead
[airplane release]you writesafeRelease(x)where safeRelease is the following macro that you add to your .pch target file:This macro doesn’t respect zombies. Here is the problem: when
NSZombieEnabledisTRUEthe object turns into aNSZombie. If you nil its object reference, any call sent to him will be ignored.To fix that, here is a macro from Kevin Ballard that sets the pointer to an invalid made up reference ONLY when
NSZombieEnabledisFALSE. This guarantees a crash during debug time if zombies are not enabled, but leaves the zombies be otherwise.References
Apple doesn’t have a recommendation on which one is best. If you want to read the thoughts of the community here are some links (the comment threads are great too):