Question

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];}
Was it helpful?

Solution

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

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top