2

I have a class for connecting with httprequests. I am getting a memory leak for "NSMutableData" altho I am releasing it in "didFailWithError" and in "connectionDidFinishLoading" of the connection object:

- (BOOL)startRequestForURL:(NSURL*)url {

[url retain];


NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
// cache & policy stuff here
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPShouldHandleCookies:YES];
NSURLConnection* connectionResponse = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
if (!connectionResponse)
{
    // handle error
    return NO;
} else {
    receivedData = [[NSMutableData data] retain]; // memory leak here!!!
}

[url release];

[urlRequest release];


return YES;}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
UIAlertView *alert =
[[[UIAlertView alloc]
  initWithTitle:NSLocalizedString(@"Connection problem", nil)
  message:NSLocalizedString(@"A connection problem detected. Please check your internet connection and try again.",nil)

  delegate:self
  cancelButtonTitle:NSLocalizedString(@"OK", nil)
  otherButtonTitles:nil, nil]
 autorelease];
[alert show];

[connectionDelegate performSelector:failedAction withObject:error];
[receivedData release];}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[connectionDelegate performSelector:succeededAction withObject:receivedData];
[receivedData release];}
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96

4 Answers4

5

The static analyser will call this a leak because you are not guaranteeing that either of the methods featuring a release will actually be called.

If you set receivedData as a retained property, and do

self.receivedData = [NSMutableData data];

Then in your dealloc (and also your didFail and didFinish, instead of the release):

self.receivedData = nil;

You will be OK.

As jbat100 points out, you are also leaking url and urlRequest if the !connectionResponse, unless you have omitted this code from the question

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks for your answer. I tried your solution and the memory leak is still here. (I had the receicvedData object in the .h file with retain. I released it in dealloc and in the connection fail/succeed I did the `self.receivedData = nil;`. I also initialized it as you said: `self.receivedData = [NSMutableData data];`. By the way - my testings are in the xcode instruments. – Yaniv Efraim Oct 05 '11 at 11:30
  • Are you doing anything with receivedData anywhere else, can you show that code too? – jrturton Oct 05 '11 at 11:52
  • I use it in connection methods: `[receivedData setLength:0];` and `[receivedData appendData:data];` – Yaniv Efraim Oct 05 '11 at 12:16
  • I've run out of ideas, sorry :( – jrturton Oct 06 '11 at 09:11
  • This question seems similar http://stackoverflow.com/questions/7672333/potential-leak-on-a-retain – jrturton Oct 06 '11 at 10:36
2

You need to make really sure that these two delegate methods are the only possible way the request could finish. I can see a leak here

if (!connectionResponse)
{
    // handle error
    return NO;
}

you do not do the release operations

[url release];
[urlRequest release];

Which you do when the connectionResponse is non-nil. On another note I strongly suggest the ASIHTTP Obj C library for doing this type of stuff.

jbat100
  • 16,757
  • 4
  • 45
  • 70
1

if you want remove this leak take NSURLConnection in .h file and release that in connectionDidFinishLoading method .reason is you are allocted NSURLConnection object there but you cann't release over there if release app kill over there .that why you have to create NSURLConnection object in .h

Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

Why do you think you are leaking? (NSMutableData) If it is because of Xcode's Analyze option; well, it lies, as it can't handle even such obvious complex situations.

However, as Narayana pointed out, you are also leaking the connection, which you should release in both the finish and fail delegate methods.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41