2

Why would I use (inside my dealloc method)?

  1. [myInstance release] instead of [self.myInstance release]
  2. myInstance = nil instead of self.myInstance = nil

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

Those practices are from numerous examples I see on the web.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
eugene
  • 39,839
  • 68
  • 255
  • 489

6 Answers6

1

Calling self.myInstance = uses the auto generated setter method. Calling [self.myInstance release]; calls release on the object returned by your getter method. It all depends on how your properties were set up (retain, assign?). There is no necessarily right or wrong answer to your question, since it all depends on the property in question. I suggest you read up on Objective C properties to get a better feel for this kind of thing.

And, unless myInstance was declared with assign, you wouldn't want to call self.myInstance = [[AClass alloc] init] You'd be much better off with self.myInstance = [[[AClass alloc] init] autorelease]

jakev
  • 2,815
  • 3
  • 26
  • 39
1

This depends on a property that you defined in interface. For example if you define retain property:

@property (nonatomic, retain) NSObject *property;

then you may use just self.property = nil; in dealloc method, because it equals to:

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

The very same thing with self.property = [[A alloc] init];. This equals to

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

in case of property = [[A alloc] init]; property won't be retained.

Here's a full properties guide form Apple.

kovpas
  • 9,553
  • 6
  • 40
  • 44
  • To @kovpas, Your link expires. Is it this one: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/codingguidelines/Articles/NamingIvarsAndTypes.html – George Nov 03 '13 at 06:31
1

1) [myInstance release] instead of [self.myInstance release]

prefer the former.

the returned value of self.myInstance is defined by implementation when a subclass has overridden the method myInstance. you're not interested in the behaviour of the interface of a constructed object during dealloc (since a subclass may override and return something other than your ivar).

what you are interested in dealloc is releasing the references you own before your object is destroyed. if the subclass has overridden myInstance, then it could:

a) return an ivar (declared in the subclass) that's already been released

or

b) the implementation of the override may return a newly created autoreleased object

either a or b could lead to an over-release and a crash (assuming everything else is correctly retained/released). this also suggests why you should assign nil to the ivar after releasing it.

this is also a classic example of how to trigger object resurrection. object resurrection occurs when an implementation of the getter/setter you call recreates its state after it's already been deallocated. the least offensive side-effect would cause a harmless leak.

2) myInstance = nil instead of self.myInstance = nil

again, prefer the former.

a formal response would look much like the response to #1 -- the rationale, side-effects and dangers apply here as well.

the safest way to handle this is to access the ivar directly:

[myInstance release], myInstance = nil;

because there may be really nasty side-effects (crashes, leaks, resurrection) which may be difficult to reproduce.

these dangers may be easily avoided and your code will be far easier to maintain. on the other hand, if people encounter the side-effects when using your programs, they will probably avoid (re)using it wherever they can.

good luck

justin
  • 104,054
  • 14
  • 179
  • 226
1

Note that using

myInstance = nil

instead of

self.myInstance = nil

Is incorrect (in the context of say a viewDidUnload method in a UIViewController subclass) if myInstance is a retain property, since if myInstance points to an object, it will be leaked!

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
0

Actually using

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

will lead in a memory leak, cause self.myInstance is using setter methods which leads in retain +1 along with alloc/init retain +1. So you'll get a retain count +2;

Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
0
... = self.myInstance

and

self.myInstance = ...

are actually subroutine or method calls to getters and setters, which depending on how you define these subroutines, or have Objective C Properties create them, could do almost anything.

If the case of retain properties, the subroutines might play with the retain counts. If you do your own getters and setters, you could have them control the lights in your house, turning them on for none zero sets and turning the lights out when setting something to zero or nil. There doesn't even need to be a backing variable named "instance" which could be set by:

instance = ...
hotpaw2
  • 70,107
  • 14
  • 90
  • 153