1

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");
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
schone
  • 225
  • 1
  • 3
  • 10
  • could you show some code? its a tad difficult to reverse engineer your code from the error message. In particular where you create the NSXMLParser would be good to see – AndersK Aug 02 '10 at 01:54
  • schone: update your original question with this code instead of pasting it as a comment. – iwasrobbed Aug 02 '10 at 02:12
  • Here's a work around: http://blog.filipekberg.se/2010/11/30/nsxmlparser-has-memory-leaks-in-ios-4/ – Filip Ekberg Nov 30 '10 at 14:33

4 Answers4

9

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...

cas74
  • 91
  • 1
  • 2
1

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

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • still getting that same leak and also getting two extra small ones saying Malloc 512 bytes at frame [allocateCollectableUnscannedStorage] ideas? – schone Aug 02 '10 at 02:31
  • have you tried without the pool, just to see if there is a diff? – AndersK Aug 02 '10 at 02:36
  • ya it crashes saying i'm trying to spawn a thread without its own pool.... it doesn't like me when i do that :) – schone Aug 02 '10 at 02:37
  • I also have another app i'm working on, just in its begining stage without a saperate thread for the NSXMLParser in which i load the parser in viewDidLoad just becuz i'm in the initial testing and and building my parser and it's giving me the same leak. This one is done on the main thread. – schone Aug 02 '10 at 02:47
  • Anyone? I'm new here, does this mean no one has any further ideas to try to tackle? – schone Aug 02 '10 at 12:44
  • Just to add more data. I'm getting the same leak when using NSXMLParser. I'm guessing its not a fault of our code but with the library. –  Jan 29 '11 at 13:24
  • It is definitely a fault of the library (as documented here and elsewhere). However, adding this call (setDelegate:nil) fixed the leak in my app - I plased it right before the [parser release] call. – geerlingguy Feb 11 '11 at 14:26
1

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.

noda
  • 11
  • 1
  • Thanks for putting in your answer. I was getting kind of worried that I'm really not getting something fundamental and it keeps on appearing in all my apps that use NSXMLParser. – schone Aug 07 '10 at 17:10
0

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];
the Reverend
  • 12,305
  • 10
  • 66
  • 121