How do I know if I need to alloc and if I need to release?
Completely new to Objective-C, trying to find out when I need to alloc and release objects.
For example, I want to fetch some data from the Web. I found an article at Apple which has this code:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc]
initWithRequest:th开发者_开发问答eRequest delegate:self];
What I don't understand is: Why do they need to call alloc on the connection but not on the request? How do I know when I need alloc and when not?
Similar questions for release. From what I read, I only ever need to release objects that were initialized using alloc/init. But all "initWithXXX" functions return autoreleased objects instead.
Is this a hard rule, or just convention? Is there a way to find out if I need to release an object or not?
Read the Cocoa Memory Management guide. You own an object only if the methods used to obtain it contains one of:
new
alloc
retain
copy
... and you only need to release
(i.e. relinquish ownership) if you own it. If you obtain an instance through a method not containing any of these, you don't own it and have to take ownership explicitly (using retain
) if you need it.
So, you need to alloc
if you:
- want ownership or
- if there is no convenience constructor available (and you thus have to use
alloc
/init
)
The initializer methods don't return autoreleased instances, they only initialize an alloc
d instance.
Please read the Memory Management Programming Guide. All of these are explained there. Also check out Learn Objective-C.
Why do they need to call alloc on the connection but not on the request?
Whenever you need to own an object to make it live longer than the function's scope, the object needs to be -retain
ed. Of course you could use
NSURLConnection* theConnection = [NSURLConnection connectionWithRequest:theRequest
delegate:self];
but the connection will be -autorelease
d. But because there's time for a connection to finish, this variable should be -retain
ed to prevent the connection becoming invalid. Of course, then, you could do
NSURLConnection* theConnection = [[NSURLConnection connectionWithRequest:theRequest
delegate:self] retain];
But this is equivalent to +alloc
→ -init
→ -autorelease
→ -retain
, the last two steps are redundant. Probably that's why Apple chooses to use +alloc
/-init
here.
(BTW, this style would cause the static analyzer to complain. It's better to store theConnection
as an ivar somewhere, e.g. in the delegate object.)
On the other hand, the NSURLRequest is just a temporary object, so it needs to be -release
d when the function ends. Again, you could use
NSURLRequest* theRequest = [[NSURLRequest alloc] initWithURL:...];
...
[theRequest release];
this is even more efficient, as the autorelease pool won't be filled up, but using this method one may forget to -release
and cause a leak.
But all "initWithXXX" functions return autoreleased objects instead.
No, -init…
should never return an -autorelease
d object.
initWithXXX:
methods do not return autoreleased objects! somethingWithXXX:
class methods, however, do. This is a convention (in that it's not compiler-enforced), but it's followed so closely that you can treat it as a hard rule. The Clang static analyzer will also complain if you don't follow it.
requestWithURL:
doesn't have "init" in the name. Also, requestWithURL:
is called on the NSURLRequest
class, whereas initWithRequest:delegate:
is called on the NSURLConenction
object returned by calling alloc
on the NSURLConnection
class.
(I hope that all makes sense.)
精彩评论