On 2010.08.18, at 2:08 PM, Sherm Pendley wrote:
Indeed, on that very page, at
<http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539-SW6 >,
it gives this example:

 - (NSString*) title {
     return [[title retain] autorelease];
 }
...

"Because the object returned from the get accessor is autoreleased in the current scope, it remains valid if the property value is changed. This makes the accessor more robust, but at the cost of additional overhead."

Yes, but on the same page, see "Technique 3" where autorelease is not used at all. The docs comment:

Its [Technique 3] disadvantage is that the old value may be deallocated immediately (if there are no other owners), which will cause a problem if another object is maintaining a non-owning reference to it.

In your example the caller has such a non-owning reference.

Implementing -stringByReplacing... as "return [[self retain] autorelease];" makes the same guarantee, that the object returned by -stringByReplacing... will remain valid if the original string is released.

Yes, but there's no guarantee which technique -stringByReplacing.. will use internally. Methods are free to use whatever implementation they feel is most appropriate. Thus one must be prepared for the original string to be returned directly, with no additional autorelease.

~Martin

_______________________________________________

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