Clarification on accessors? (was: Yet another memory management question)
Clarification on accessors? (was: Yet another memory management question)
- Subject: Clarification on accessors? (was: Yet another memory management question)
- From: Caleb Strockbine <email@hidden>
- Date: Wed, 08 Jul 2009 12:51:50 -0400
On Jul 4, 2009, at 9:10 PM, mmalc wrote:
If you manipulate an instance variable anywhere other than in an
initialiser or a dealloc method, you should use a suitable accessor
method.
On Jul 5, 2009, at 6:53 AM, mmalc wrote:
On Jul 4, 2009, at 9:40 PM, WT wrote:
[...]
There is plenty of Apple sanctioned code that does not follow that
recommendation, both in sample code and in documentation.
Where instance variables are set without using accessor methods, this
should be regarded as a bug in the documentation or sample code and
reported.
Use of accessor methods is described plainly here:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW4
Those are some pretty strong statements, so I wonder whether this
reflects some new advice from Apple, or if it's perhaps just a bit of
an overstatement. Don't get me wrong, I'm all for using accessors for
several reasons: the linked document clearly explains how they help
avoid common memory management errors; they give you KVC compliance
almost automatically; and they provide a level of abstraction which
lets you change a class' implementation without breaking clients of
that class. Those are all fine reasons, but they don't always apply.
Is it really the case that we should ALWAYS use accessors, even for
simple (i.e. non-pointer), private variables? Are there advantages to
using accessors that I'm not aware of, like a simpler transition to
Grand Central Dispatch, 64 bit processing, etc? Or is it just simpler
to advocate accessors in all cases than to give more nuanced advice?
On Jul 4, 2009, at 9:37 PM, mmalc wrote:
In an initaliser method and in dealloc, you should typically set or
release the variable directly, so in dealloc it would be
[myArray release];
Really? Are -init and -dealloc special in some way that makes it a bad
idea to use accessors? If you've got an accessor for myArray, why
wouldn't you just say:
self.myArray = nil?
Thanks for any feedback on this. And because this is the Internet, I
should probably add that although I've called out three statements by
mmalc, I certainly don't mean for this to be any sort of attack on
him! It's quite the opposite: mmalc and some others here post so much
helpful information here that I find it disturbing when my own mental
model is inconsistent with that advice.
cheers,
-Caleb
_______________________________________________
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