Re: NSString componentsSeparatedByString returned memory
Re: NSString componentsSeparatedByString returned memory
- Subject: Re: NSString componentsSeparatedByString returned memory
- From: "Sherm Pendley" <email@hidden>
- Date: Fri, 14 Dec 2007 16:58:08 -0500
On Dec 14, 2007 4:29 PM, Martin Linklater <email@hidden> wrote:
> Hi - I'm drilling down on memory leaks in my code and I'm finding some
> strange memory usage that I don't quite understand. I'm calling
> 'componentsSeparatedByString' on an NSString and I seem to be finding
> a memory leak with the NSArray that is returned.
>
> If I release the NSAutoreleasePool after the call the memory for the
> NSArray is not free'd up. If I manually release the NSArray I get a
> double free error later on in the code when the NSAutoreleasePool is
> released.
>
> This doesn't make sense - you either release objects yourself or you
> let the NSAutoreleasePool do it right ? With this case I seem to
> either get a leak or an error - neither method seems to properly clean
> the memory up.
>
> Am I missing something subtle here ? Thanks.
Sort of. The -componentsSeparatedString: method is one for which you are not
responsible for releasing the return value - you obviously understand that
part. However, that's where the contract ends; whether it's autoreleased, or
released later by some other mechanism, is an implementation detail that you
shouldn't worry about. The only thing you can depend on for certain is that
you don't need to release the returned array yourself.
If releasing the array yourself results in a double free error, that
indicates that *something* is eventually releasing it, just as it should.
That means there's no leak; if there were - i.e. if it weren't being
released properly - you wouldn't get that error.
sherm--
_______________________________________________
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