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 15:49:57 -0400
On Wed, Aug 18, 2010 at 3:33 PM, Kyle Sluder <email@hidden> wrote:
> On Wed, Aug 18, 2010 at 12:16 PM, Sherm Pendley <email@hidden> wrote:
>> Possibly, but even in that case it would still have to "return [[self
>> retain] autorelease];" in order to fulfill its end of the memory
>> management contract.
>
> This is incorrect. It's perfectly valid to just return self if you
> aren't returning a +1 reference.
Not true. Consider this example, where
-stringByReplacingOccurrencesOfString:withString: could reasonably
take a shortcut and return self:
NSString *foo = [[NSString alloc] initWithString:@"foo"];
NSString *bar = [foo stringByReplacingOccurrencesOfString:@"foo"
withString:@"foo"];
[foo release];
The "[foo release]" is perfectly correct, but if
-stringByReplacingOccurrencesOfString:withString: is implemented with
a simple "return self;" then *both* foo and bar would be immediately
released. Implementing it as "return [[self retain] autorelease]"
prevents such problems.
sherm--
--
Cocoa programming in Perl:
http://www.camelbones.org
_______________________________________________
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