Why is myInstance = nil instead of self.myInstance = nil?

Why should I use (inside my dealloc method)?

  • [myInstance release] instead of [self.myInstance release]
  • myInstance = nil instead of self.myInstance = nil

Although we use self.myInstance = [[[AClass alloc] init] autorelease] instead of myInstance = [[[AClass alloc] init] autorelease] ?

These practices are taken from numerous examples that I see on the Internet.

+2
source share
6 answers

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

+1
source

The call self.myInstance = uses the auto-generator method. Call [self.myInstance release]; causes the release of the object returned by your getter method. It all depends on how your properties were configured (save, assign?). There is no right or wrong answer to your question, since it all depends on the property in question. I suggest you familiarize yourself with Objective-C properties to better understand this.

And, if myInstance has not been declared with an appointment, you will not want to call self.myInstance = [[AClass alloc] init] You will be much better off with self.myInstance = [[[AClass alloc] init] autorelease]

+1
source

It depends on the property that you defined in the interface. For example, if you define a save property:

 @property (nonatomic, retain) NSObject *property; 

then you can use only self.property = nil; in the dealloc method because it is equal to:

 [property release]; // releases previous property property = [nil retain]; // [nil retain] returns just nil 

Same thing with self.property = [[A alloc] init]; . It is equal

 [property release]; // releases previous property property = [[[A alloc] init] retain]; 

in the case of property property = [[A alloc] init]; will not be saved.

Here is the complete Apple Property Guide form.

+1
source

Please note that use

 myInstance = nil 

instead

 self.myInstance = nil 

Wrong (in the context of, for example, the viewDidUnload method in a subclass of UIViewController) if myInstance is a save property, because if myInstance points to an object, it will leak!

+1
source

Actually using

 self.myInstance = [[AClass alloc] init]; 

will lead to a memory leak, because self.myInstance uses setter methods, which lead to a save of +1, along with alloc / init, keep a +1. Thus, you will get a +2 counter;

0
source
 ... = self.myInstance 

and

 self.myInstance = ... 

are actually routines or method calls for getters and setters, which, depending on how you define these routines or create them Objective-C Properties, could do almost everything.

If, in the case of stored properties, subroutines can be reproduced while maintaining the samples. If you make your own getters and setters, you can make them control the lights in your home by turning them on without any zero sets and turning off the lights, setting something to zero or zero. There should not even be a reference variable named "instance" that can be set:

 instance = ... 
0
source

All Articles