On Wed, Aug 18, 2010 at 5:23 PM, Kyle Sluder <kyle.slu...@gmail.com> wrote:
> On Wed, Aug 18, 2010 at 2:08 PM, Sherm Pendley <sherm.pend...@gmail.com> 
> 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:
>
> It gives it as one example of three, the other two not having
> performing an immediate retain/autorelease pair.

The second example avoids having a retain/autorelease in the getter,
by moving the autorelease to the setter instead; while valid for
accessors, it's not really applicable to -stringByReplacing..., since
that's not an accessor method.

The third example avoids autorelease, but has precisely the same
problem that "return self" would have:

"Its 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. For example:

  NSString *oldTitle = [anObject title];
  [anObject setTitle:@"New Title"];
  NSLog(@"Old title was: %@", oldTitle);

If anObject was the only object that owned the original title string,
then the string will be deallocated after the new title is set. The
log statement would then cause a crash as oldTitle is a freed object."

sherm--

-- 
Cocoa programming in Perl:
http://camelbones.sourceforge.net
_______________________________________________

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