Memory leak

Discussion in 'Mac Programming' started by shilpa, Jul 3, 2008.

  1. shilpa macrumors member

    Joined:
    May 29, 2008
    #1
    Hi all,
    in my app ,i am parsing xml files using NSXMLParser.to connect to url we are making use of NSURLConnection.we are using synchronous connection.this will return NSData type. In performance tool i am checking for leaks.

    - (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
    {


    theRequest=[NSURLRequest requestWithURL:URL];

    NSError *connectionError= nil;
    receivedData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&connectionError];


    NSLog(@"\n\n---------Succeeded! Received %d bytes of data",[receivedData length]);

    if([receivedData length] != 0)
    {
    //catalogObj = [Catalog getCatalogObject];

    @try
    {

    parserObject = [[NSXMLParser alloc] initWithData:receivedData];

    //receivedData = nil;
    //theRequest = nil;
    //response = nil;
    }
    @catch (NSException *e)
    {
    NSLog(@"ERROR : Name: %@\nReason: %@\n\n",e.name,e.reason);
    }

    [self.parserObject setDelegate:self];
    [self.parserObject setShouldProcessNamespaces:NO];
    [self.parserObject setShouldReportNamespacePrefixes:NO];
    [self.parserObject setShouldResolveExternalEntities:NO];


    @try
    {
    [self.parserObject parse];
    }
    @catch (NSException * e)
    {
    NSLog(@"ERROR : Name: %@\nReason: %@\n\n",e.name,e.reason);
    }
    @finally
    {

    NSError *parseError = [self.parserObject parserError];
    if (parseError)
    {

    NSLog(@"\n------------------");
    NSLog(@"\nPARSE ERROR OCCURED.....\nDescription: %@\nReason: %@\nSuggestion: %@\nInfo: %@\n\n",[parseError localizedDescription],[parseError localizedFailureReason],[parseError localizedRecoverySuggestion],
    [[parseError userInfo] objectForKey:NSErrorFailingURLStringKey]);
    NSLog(@"\n------------------");
    }

    [parseError release];

    //[parserObject release];

    }
    }




    }



    it is showing leak peak.when i click on there it will show me leaked blocks
    and it will go to
    receivedData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&connectionError];
    this line of code in my app.

    so how to release memory occupied by this.im releasing receivedData in my dealloc funcn,but still there is leak so how to solve it.
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I think maybe you should be releasing receivedData after you use it by handing it to the parser object. Or you could even do it before that assignment, but the point is you need to release it once each time that method is called, because you're getting a new NSData object each time. I'm not sure if sendSynchronousRequest:returningResponse:error: hands you an autoreleased object but I don't think so. So what happens is you assign some memory and don't release it in that method. The next time that method is called, you reassign the same pointer to a new block of memory (NSData object). That memory from the previously-assigned NSData is now leaked because nothing has a reference to it anymore and it was never released. When you release it in dealloc:, you're only releasing the last block you made the last time you called your parsing method. Does that make sense?
     

Share This Page