views:

922

answers:

4

I'm seeing an intermittent crash on [parser release]. I'd say I see it about 5% of the time, and the data I am parsing varies between each crash. I can't for the life of me figure out why.

Before I submit a bug report to Apple (which, with my luck, will not be reproducible in sample code), has anyone run into this and know what might be going on?

    NSData *d = [data copy]; // data is typically 2K-13K bytes
    @synchronized (xmlParserLock) {
        [[NSURLCache sharedURLCache] setMemoryCapacity:0];
        [[NSURLCache sharedURLCache] setDiskCapacity:0];

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:d];
        [parser setDelegate:self];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
        [parser release];
        [pool release];
    }
    [d release];

And here's the gdb 'where' output, which points to [parser release]:

#0  0x93d08d12 in xmlCharEncCloseFunc ()
#1  0x93cfc0e3 in xmlFreeParserInputBuffer ()
#2  0x93cfc08f in xmlFreeInputStream ()
#3  0x93cfbdac in xmlFreeParserCtxt ()
#4  0x961384d6 in -[NSXMLParser dealloc] ()
#5  0x00149de7 in -[MyParserClass parseResponse] (self=0x104e9f0, _cmd=0x1766dc) at /Users/mike/Documents/MyApp/Classes/MyParserClass.m:60

Thanks in advance for any help!

A: 

It looks like you are using multiple threads. There could be an issue with that. Threading bugs often do present themselves sporadically. You might also have a bug in your parser delegate methods, which you didn't post here.

Chris Lundie
I've read that NSXMLParser doesn't do well with multiple threads, so yes, I've synchronized around its allocation and deallocation. As far as the NSXMLParser is concerned, this should remove the multiple threads angle of the problem, I would think.
Mike McMaster
As for the delegate methods, I just don't see what I could be doing there that would tie in with why a bad access happens on deallocating the NSXMLParser. It's pretty strange. Thanks for your comments.
Mike McMaster
+1  A: 

I think I figured it out - some code elsewhere in the app uses XML functions such as:

xmlCtxtReadMemory()
xmlClearParserCtxt();
xmlFreeParserCtxt();
xmlCleanupParser();
xmlFreeDoc();

These functions are likely executing in another thread at the same time I am executing the code fragment I posted. NSXMLParser obviously uses the same functions under the hood.

I've added a synchronized block to the other code using the same lock object as the one I use for my NSXMLParser usage, and the crashes seem to have gone away. So I guess the lesson here is that these XML functions are totally not thread-safe - use with caution!

Mike McMaster
A: 

You need to delete your data only when parsing is complete. In the delegate method called parserDidEndDocument: release data.

Hope this help.

thierry

A: 

You need to delete your data only when parsing is complete. In the delegate method called parserDidEndDocument: release data.

Hope this help.

thierry

thierryb