Tutorial :NSXMLParser memory leak on ios 4.0 - **NOT** NSCFString



Question:

I'm getting a weird leak in my NSXMLParser after it's done and released from memory.

It comes up with NSMapTable alloc leak. Here's my stack:

   0 libSystem.B.dylib calloc     1 libobjc.A.dylib _internal_class_createInstanceFromZone     2 libobjc.A.dylib class_createInstance     3 Foundation NSAllocateObject     4 Foundation +[NSMapTable alloc] <===== this is the leak...     5 Foundation -[NSXMLParser initWithData:]     6 Foundation -[NSXMLParser initWithContentsOfURL:]     7 idispatch -[RootViewController parseXML:] /Developer/iPhone  Apps/iDispatch/Classes/RootViewController.m:562 <================== this is my code calling     8 Foundation -[NSThread main]     9 Foundation __NSThread__main__    10 libSystem.B.dylib _pthread_start    11 libSystem.B.dylib thread_start  

Ideas?

Appreciate any light you can shed!

Here's the code:

[NSThread detachNewThreadSelector:@selector(parseXML:)                            toTarget:self                          withObject:requestStr];  

which calls this method on its own thread:

- (void)parseXML:(NSString*)theURL {      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]];      DLog(@"URL: %@",theURL);      [parser setDelegate:self];        [parser parse];        [parser release];        [pool release];      DLog(@"xml parser thread end and released");  }  


Solution:1

It's perhaps too late but I found this solution :

NSData * dataXml = [[NSData alloc] initWithContentsOfURL:url];  NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataXml];  [dataXml release];  

instead of

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];  

No more mem leaks...


Solution:2

You should reset the delegate before releasing ([parser setDelegate:nil])


Solution:3

Same problem here. The leak appears even if I'm just to this:

NSURL *xmlURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfile.xml"]];   NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];  [parser release];  

I've reported the bug to apple, since it seems a serious bug in the NSXMLParser class.


Solution:4

I have the same problem, Malloc 512 Bytes & NSConcreteMapTable Leaks... also this code does not leak if compiled against Snow Leopard 10.6

I can also confirm that the following code works for ios & mac os without leaks.

        NSData * dataXml = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:query]];      NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataXml];      [parser setDelegate:self];      [dataXml release];      [parser parse];      [parser autorelease];  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »