Re: stringByReplacingCharactersInRange leading to bus error
Re: stringByReplacingCharactersInRange leading to bus error
- Subject: Re: stringByReplacingCharactersInRange leading to bus error
- From: Sherm Pendley <email@hidden>
- Date: Wed, 18 Aug 2010 17:35:08 -0400
On Wed, Aug 18, 2010 at 5:23 PM, Kyle Sluder <email@hidden> wrote:
> On Wed, Aug 18, 2010 at 2:08 PM, Sherm Pendley <email@hidden> 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 (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