• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Clarification on accessors? (was: Yet another memory management question)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Clarification on accessors? (was: Yet another memory management question)


  • Subject: Re: Clarification on accessors? (was: Yet another memory management question)
  • From: Erik Buck <email@hidden>
  • Date: Wed, 8 Jul 2009 11:20:48 -0700 (PDT)

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Clarification on accessors? (was: Yet another memory management question)
      • From: "Sean McBride" <email@hidden>
  • Prev by Date: Re: [NSCachedImageRep release]
  • Next by Date: Re: CoreData deleteAll of kind
  • Previous by thread: Re: [NSCachedImageRep release]
  • Next by thread: Re: Clarification on accessors? (was: Yet another memory management question)
  • Index(es):
    • Date
    • Thread