views:

83

answers:

2

I have follwowing peace of code in which I have specified the leaky line . As I am new to iPhone developement I can't understand what actually is wrong with that line . please have a look at that line and tell me .

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    //take away
    //NSURL *url1 = [[NSURL alloc] initWithString:@"http://url/Service.asmx/takeAwayList"];
    NSURL *url1 = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@/Service.asmx/takeAwayList",serviceURL]];

    NSMutableURLRequest* request1=[NSMutableURLRequest requestWithURL:url1];
    [request1 setHTTPMethod:@"POST"];
    [request1 setTimeoutInterval:10];

    //*****the leaky line***********************///
    NSData *data2=[[NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil] autorelease];

    if(data2 == nil)
    {
     UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];
     [alert release];

    }
    else
    {

     NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithData:data2];

     //Initialize the delegate.
     TakeAwayParser *takeAwayParser = [[TakeAwayParser alloc] initTakeAwayParser];

     //Set delegate
     [xmlParser1 setDelegate:takeAwayParser];

     //Start parsing the XML file.
     @try {
      BOOL success = [xmlParser1 parse];
      if(success)
       NSLog(@"No Errors");
      else
       NSLog(@"Error Error Error!!!");
     }
     @catch (NSException * e) {
      NSLog(@"Exception in parsing %@  %@",[e name], [e reason]);
     }
     [takeAwayParser release];
     [xmlParser1 release];
    }

    //[request1 release];
//  [response1 release];
//  
    [url1 release];
//  [data2 release];
    //new arrivals
    //[data2 release];
    [pool release];
+1  A: 

This line isn't leaking, you shouldn't even being autoreleasing it.

Do yourself a favor and read the Memory Management Guide in Apple's developer docs, commented out releases in your code do not bode well.

EDIT: Hrm I take that back your code is completely fine except for that one line. Are you sure that it's leaking? It's returning an object with a retain count of 0, so you autoreleasing it should be causing trouble because it already has a retain count of 0.

refulgentis
So why instruments is taking me on that line every time ?
hib
If I will remove the autorelease then also the instruments is guiding me on that line . Is it something that I have read somewhere that instruments gives some time false positives or fake leaks ?
hib
+2  A: 

I had issues with this as well in my Large project. After working with an Apple engineer on trying to locate the leaks, he finally asked the main Apple dev team behind NSURLConnection. They basically said that there is an internal cache that is not clearable at all in NSURLConnection and it was a known issue.

So I set out looking for alternatives. I found ASIHTTPConnection (link below) which works off of CFNetwork. It is designed to be a drop-in replacement for NSURLConnection, plus a bunch of other awesome goodies like downloading to disk instead of memory, download resuming, progress bar callbacks etc..

I have used it in all my projects and have never had any issues or complaints. An, in answer to your question, this is how I got rid of those memory leaks.

http://allseeing-i.com/ASIHTTPRequest/

coneybeare
This is silly but I am still in learning phase so can you specify what should I take from the Page you have redirected and What should I replace in my code ?
hib
I think this little code also works . for now the instruments is not giving me any further leaks on url after putting this code:NSMutableURLRequest* request1=[NSMutableURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0]; [request1 setHTTPMethod:@"POST"]; //[request1 setTimeoutInterval:10]; [[NSURLCache sharedURLCache] setMemoryCapacity:0]; [[NSURLCache sharedURLCache] setDiskCapacity:0];
hib