views:

34

answers:

3

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:theRequest 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?

+1  A: 

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.)

Wevah
+1  A: 

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 allocd instance.

Georg Fritzsche
+1  A: 
KennyTM
All three answers are really helpful, accepting this as it's the most complete one.
Michael Stum