Actual use of accessories in init and dealloc methods?

I have already heard from several sources (stackoverflow.com, cocoa -dev, documentation, blogs, etc.) that it is "wrong" to use accessors and settings (foo, setFoo :) in your init and dealloc. I understand that there is a remote possibility to confuse other objects that observe the property if you do. (a simple example is given here below)

However, I must say that I disagree with this practice for the following reason:

The new Objective-C environment (one on the iPhone and the 64-bit runtime in 10.5) allows you to declare properties without declaring the corresponding ivar. For example, the following class will compile just fine on 10.5 or for the iPhone (device, not simulator):

@interface Foo : NSObject { } @property (retain) id someObject; @end @implementation Foo @synthesize someObject; @end 

Understanding that the above is a perfectly valid Objective-C class, let's say I decided to write an initializer, and for the purposes of memory management, the dealloc method (since GC is not available on iPhone). All I have ever read about initializers and release would make me write the following two methods:

 - (id) init { if (self = [super init]) { //initialize the value of someObject to nil [self setSomeObject:nil]; } return self; } - (void) dealloc { //setting someObject to nil will release the previous value [self setSomeObject:nil]; [super dealloc]; } 

However, according to the documentation and popular opinion, this is "wrong." So my questions are these:

  • How can I initialize someObject without using access? You could say that the compiler (or the runtime or something else) guarantees that someObject is already set to zero, but I believe that it would be wrong behavior to rely on this. Having a decent background in C, I saw a lot of errors due to incorrect variable initialization, and this seems a bit different.
  • How can I free someObject if I should not use accessor in the dealloc method?

If the answer to any of them is “you cannot,” then how bad will it be to use accessors in your init and dealloc methods?

+24
initialization properties objective-c accessor dealloc
Aug 16 '09 at 4:08
source share
2 answers

I understand that the current 10.5 behavior, in which synthesized ivars are not directly accessible, is considered by Apple to be a mistake; You should be able to access it directly, but you cannot.

Therefore, you should be able to:

 someObject = nil; 

instead

 self.someObject = nil; 

In the meantime, using an accessor directly is the only way to do this without providing an explicit ivar.

Update : this bug has been fixed; now you can do someObject = nil just fine.

+8
Aug 16 '09 at 4:13
source share
— -

EDIT (February 13, 2013): As noted in my comment below, and especially after adding ARC, I changed my mind about this. Before ARC, I saw a lot of errors causing failures due to incorrect ivar assignments in init . IMO, especially when working with junior teams, the rare problems with using accessories in init have been outweighed by common ivar access errors. Because ARC fixes these errors, errors related to the rare but possible using accessor in init may be more important, and so I switched to supporting the direct use of ivars in init and dealloc , and only in those places; (perhaps you cannot use accessors inside the accessor itself).




Answer PRE-ARC

I strongly disagree with those who object to accessors in -init . In almost all cases, this is a very good place to use accessories, and it eliminates the many errors that I saw in the new Cocoa codes, which invariably do not persist when assigned to -init .

-dealloc is a harder call. I have a natural tendency to use accessors there (so that they are used everywhere), but this can cause a headache due to KVO (or even NSNotifications if you post a change notification on your setter). However, although I do not use accessors in -dealloc , I find this very controversial, and Apple is very incompatible with this (we know that they call setView: in UIViewController -dealloc for example).

In any case, I would say that the underutilization of accessories caused 100% overuse errors. I will always be mistaken in using them, unless there is a good reason not to.

+9
Aug 17 '09 at 5:03
source share



All Articles