1) [myInstance release] instead of [self.myInstance release]
prefers the first.
the return value of self.myInstance determined by the implementation when the subclass overrides the myInstance method. you are not interested in the behavior of the interface of the constructed object during dealloc (since the subclass may override and return something other than your ivar).
what interests you dealloc frees up the links that you own before your object is destroyed. if the subclass redefined myInstance , then it could:
a) return the already declared ivar (declared in the subclass)
or
b) an override implementation may return a newly created object with auto-implementation
either a or b can lead to over-release and failure (provided that everything else is properly saved / released). this also suggests why you should assign nil to ivar after its release.
This is also a classic example of how to cause an object to resurrect. resurrection of an object occurs when the implementation of the receiver / setter that you call recreates its state after it is already freed. The least offensive side effect will cause a harmless leak.
2) myInstance = nil instead of self.myInstance = nil
prefer the first one.
the formal answer will be similar to the answer to # 1 - there may also be justifications, side effects and dangers.
The safest way to handle this is to contact ivar directly:
[myInstance release], myInstance = nil;
because there can be really unpleasant side effects (crashes, leaks, resurrection) that can be difficult to reproduce.
these dangers can be easily avoided, and your code will be much easier to maintain. on the other hand, if people encounter side effects when using your programs, they probably avoid (re) using wherever they are.
luck
justin
source share