views:

121

answers:

2

Hi, I'm following the official Apple sample http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

While examining my code with Instruments I found a leak, caused by the non released NSURLRequest. I've now included a [request release] shortly after NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];

and it seems to work. Am I right?

Regards

+2  A: 

The request is autoreleased which means that the autorelease pool should take care of releasing it. If you are using that within a Cocoa application you should have an autorelease pool setup automatically that will handle the freeing of autoreleased objects.

If you are working on a non-cocoa app you will have to set that up yourself and make sure to release/drain the pool in order to free your objects.

Generally you only need to release objects created with alloc, copy, mutableCopy or if you sent it retain. That means you will have to release the connection object that you created with [[NSURLConnection alloc] init... or it will cause a leak.

Here is the link to a document about Autorelease Pools

m5h
A: 

Ah, I see. For some reasons I've mixed the "official" sample with my own realization :) In my code I was doing this:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];

and this has of course to be released. The sample I mentioned above behaves different. Seemed to be rather late last night.

However, many thanks for your answer.

One thing drives me crazy: I don't get an email notification, if someone answers to my questions, although I've set the marker...

Regards

neil