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!
I think I figured it out – some code elsewhere in the app uses XML functions such as:
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!