1

I am getting memory leak in instruments in the codeenter image description here

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

     NSMutableString * res = [[[NSMutableString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]autorelease];
     [webData release];
    [connection release];
        [res replaceOccurrencesOfString:@"&" withString:@"&" options:NSCaseInsensitiveSearch range:(NSRange){0,[res length]}];
    [delegate getcat:res];

    [pool drain];
}



- (void)getcat:(NSString*)xml
{

if (xmlParser) {

    [xmlParser release];
}
Cid = [[NSMutableArray alloc] init];
Categories = [[NSMutableArray alloc] init];

NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
xmlParser = [[NSXMLParser alloc] initWithData:data];

[xmlParser setDelegate:self];

[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[xmlParser release];

}

Is this the correct way to manage memory?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Rams
  • 1,721
  • 12
  • 22

2 Answers2

1

Instead of setting up an autorelease pool, which will actually release the string, why don't you just release it yourself? If the delegate retains the string in getcat:, you can simply release it:

- (void) connectionDidFinishLoading: (NSURLConnection *) connection
{
    // Omit the autorelease pool.

    NSMutableString * res = [[NSMutableString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    [webData release];
    [connection release];
    [res replaceOccurrencesOfString:@"&" withString:@"&" options:NSCaseInsensitiveSearch range:NSMakeRange(0, res.length)];
    [delegate getcat:res];
    [res release];
}

Taking a look at getcat:, I see a problem:

[xmlParser parse];
[xmlParser release];

Usually, objects need a delegate to return results from a thread. I assume that [xmlParser parse] starts a thread. You should probably not release it before it is finished, i.e. you do that in parserDidEndDocument:.

This does however not explain the many leaked strings.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • ...When i am frequently accessing nsurlconnection . i got a leak in the string – Rams Aug 29 '11 at 13:30
  • That leak is not caused by your code above, and an autorelease pool used that way won't do any good either. – Rudy Velthuis Aug 29 '11 at 13:57
  • You could add `NSLog(@"initialized %p", res);` and `NSLog(@"released %p", res);` calls everywhere and see if the retains and releases match (`%p` gives an address). – Rudy Velthuis Aug 29 '11 at 14:01
  • @ Rudy Velthuis (initialized 0xd32a1c0,released 0xd32a1c0) how can i identify the leak. – Rams Aug 29 '11 at 14:32
  • @Rams: If you only get that once, you don't seem to have a leak there. Put such log calls everywhere you initialize or release a string. I thought you called this routine very often? If so, you should get a lot of log messages. Also use the allocs instrument together with the leaks instrument and see what got alloced where. – Rudy Velthuis Aug 29 '11 at 15:57
  • You are coorrect the leak is not in the line. the leak is in xml parsing page – Rams Aug 31 '11 at 13:57
0

I fixed this issue

The leak is in

  • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    currentElement = [elementName copy];--->Always leaking in this line But leak instrument show that line

}

replace the code with self.currentElemnt=elementName

Rams
  • 1,721
  • 12
  • 22