Reputation: 6247
I am in an argument with a friend who says that I have to put autorelase here:
NSURL *url = [[NSURL URLWithString:@"http://origin-www.metrolyrics.com/api/widgets/mac/seeker.php"] autorelease];
But isn't the object automatically autoreleased when it was send to me from NSURL class method? Thanks.
Upvotes: 1
Views: 212
Reputation: 243146
This is the rule:
If you invoke a method that returns an object and:
new
alloc
retain
copy
then you are responsible for releasing (or autoreleasing) the returned object. The only time this would not be the case is if the documentation says otherwise. You may also see in the header files that the method is annotated with the NS_RETURNS_RETAINED
macro. (The header file counts as documentation)
An easy way to remember this is "NARC" (new-alloc-retain-copy).
In your example, since URLWithString:
does not begin with new
or alloc
, does not contain copy
, and is not retain
, then you must not release the returned object. Doing so is a violation of the memory management guidelines and will cause your app to crash (unless you're doing something stupid elsewhere).
Upvotes: 5
Reputation: 3051
You are right. you did not allocate or copy an object so you do not have to release it. Just use your code without the autorelease. ;-)
The Class method you used looks like this: (Normally it does. We can't know it, because Apple doesn't share the code.)
+(id)URLWithString:(NSString *)aString {
return [[[self alloc] initWithString:aString] autorelease];
}
Upvotes: 4