On Feb 21, 2009, at 9:22 AM, Ken Thomases wrote:

On Feb 21, 2009, at 12:39 PM, Stuart Malin wrote:

I've seen the idiom [[property retain] autorelease] used in getter accessors, and just re-read the Memory Management Programming Guide (MMPG) to understand this. Now I do, and see how the perhaps once canonical approach of having a setter release-then-retain (if the new value is not equal to the present value) can lead to references persisting to a released object (e.g., if the setter is invoked after the getter has issued references). In fact, the MMPG goes so far as to say this about this approach:

"...because of the potential dangers of invalidating objects prematurely, use of this technique should be used sparingly and well documented."

Hmm. Google finds the Accessor Methods article of the Memory Management Guide when I search for that phrase, but the article doesn't actually contain the phrase. I guess it's been edited out.

In any case, I'm not aware of any such issue with any of the techniques listed on the page now.

Ah, I had a pdf copy of Memory Management Programming Guide dated 2008-02-08. The developer site now has one dated 2009-02-04, and sure enough, that statement has been removed.

So, I am curious as to why the default (retain) behavior of @synthesize does just this?

What makes you think it does? There is no concrete promise about how synthesized accessors are implemented, except that they conform to the declared property attributes (retain vs. copy vs. assign, etc.).

From "The Objective-C Programming Language" pdf (dated 2009-02-04) page 59:

Setter Semantics
These attributes specify the semantics of a set accessor. They are mutually exclusive.
assign
Specifies that the setter uses simple assignment. This is the default.
retain
Specifies that retain should be invoked on the object upon assignment. ( The default is assign.)
The previous value is sent a release message.

Note the last sentence from the above.

But as mmalc pointed out, the behavior is a function of atomicity. Further from the quoted document (page 60):

If you do not specify nonatomic, then in a reference counted environment a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value — the implementation will be
similar to the following:
[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

Because by default accessors are atomic, this seems to me to be somewhat contrary to the earlier quote.


There is one place where some pseudo-code is used to _illustrate_ what _might_ be going on inside a synthesized setter, but the documentation clearly says that the actual implementation may differ.


And, is there a way to tell @synthesize to use one of the other approaches, or do I need to make such a property @dynamic and handle the setter/getter myself?

You should trust that Apple is synthesizing just about the most appropriate setter imaginable, given the declared property attributes. Unless you need to do something else within the setter -- that is, the setter has additional side effects -- just use @synthesize.

Cheers,
Ken


_______________________________________________

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

Reply via email to