I have re-written the Accessors chapter of "Cocoa Design Patterns" several times because of controversy over whether accessors should, can, must or must not be used in initializers and dealloc. The bottom line is that accessors are the only way to set synthesized instance variables to nil in the modern (64bit) Objective-C runtime. Furthermore, you must use accessor for properties that are not backed by instance variables e.g. properties stored in axillary dictionaries etc. Accessor must be used in initializers and dealloc in at least some cases. I like consistency, and I use accessors within initailizers and dealloc. An excerpt of my current recommendation (especially a note:) follows:
Confining Memory Management to Accessors If the Accessors pattern is -[if supportFields]>consistently applied, almost all memory management for objects can be confined to accessors. When initializing object instances, use a set accessor to set the value of each object property. For example, the following implementation of -(id)initWithStringValue:(NSString *)aValue method uses an accessor to store the string value rather than making a direct assignment to an instance variable: - (id)initWithStringValue:(NSString *)aValue { self = [super init]; [self setStringValue:aValue]; // set the initial value of the property return self; } The process of initializing instances is described in Chapter 3, "Two-Stage Creation." The -dealloc method can indirectly release referenced objects using the set accessors and avoid memory management code in its implementation as follows: - (void)dealloc { [self setStringValue:nil]; // any previous string value is released [super dealloc]; } Note Some programmers have historically avoided using accessors within initializer methods and -dealloc because a subclass may override inherited accessors to cause side effects. Using the overridden accessors within the superclass’s initializer might invoke side effects before the subclass instance is fully initialized. Similarly, accessors called from within the superclass’s -dealloc may cause side effects in partially deallocated instances. However, there is no practical alternative to using accessors when you use synthesized instance variables with the modern Objective-C 2.0 runtime or use properties that are not implemented as instance variables. In such cases, accessors provide the only way to initialize the properties or set the properties to nil. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com