Re: Object Ownership
Re: Object Ownership
- Subject: Re: Object Ownership
- From: Graham Cox <email@hidden>
- Date: Mon, 24 May 2010 10:34:11 +1000
On 23/05/2010, at 1:18 AM, Michael Jackson wrote:
> Hi all,
>
> First day with Cocoa, so please excuse the basic newbness of this
> question. ;) I've read through the documentation about object
> ownership and disposal and just wanted to make sure I'm doing
> everything correctly in the following method.
>
>
> - (NSURL *)makeURL:(NSString *)aURLString
> {
> NSMutableString *aCopy = [aURLString mutableCopy];
>
> // do some stuff with aCopy here
>
> NSURL *aURL = [NSURL URLWithString:aCopy];
> [aCopy release];
>
> return aURL;
> }
>
>
> The question is whether or not NSURL's URLWithString: is going to
> retain aCopy, so it's safe to be released on the next line. I'm just
> looking for some confirmation or any pointers from people who are more
> used to managing memory than I am.
Ownership-wise, your code is OK. <aCopy> is yours - you created it using a method containing the word 'copy', therefore you own it, and so it's your job to release it when you've finished with it, as you're doing.
What I don't understand is why you make the copy.
[NSURL URLWithString] doesn't require a mutable string, it requires a string, so your whole method could be reduced to:
- (NSURL*) makeURL:(NSString*) aURLString
{
return [NSURL URLWithString:aURLString];
}
But since this is not adding any value to the framework method, why not just invoke [NSURL URLWithString:] at the point that the client is calling your -makeURL: method?
--Graham
_______________________________________________
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