0

In my application, i am downloading the XML data from server in a background thread, and do the parse and update the database tables

The background thread will be created during application start and keep running.

But the problem is during NSXMLParser Object release, i am getting EXC_BAD_ACCESS (SIGSEGV) Here is the XML Parser code:

    - (void)parseXMLWithData:(NSMutableData *)pObjXMLBufferPtr
    {
        @try
        {
             [[NSURLCache sharedURLCache] setMemoryCapacity:0];
            [[NSURLCache sharedURLCache] setDiskCapacity:0];

            NSMutableData   *lTempData = [pObjXMLBufferPtr copy];
             NSXMLParser *lObjXMLParserPtr = [[NSXMLParser alloc] initWithData:lTempData];
            [lTempData release];

           [pObjXMLParserPtr setShouldResolveExternalEntities: YES];
           [pObjXMLParserPtr setDelegate: m_cObjSAXHandler];
           //m_cObjSAXHandler is my custom class. here is declaration SAXHandler :      
           NSObject<NSXMLParserDelegate>
          [pObjXMLParserPtr parse];

            [lObjXMLParserPtr setDelegate:nil];
            [lObjXMLParserPtr release];
            lObjXMLParserPtr = (NSXMLParser *)nil;
        }
        @catch (NSException *ex)
        {
               NSLog(@"parseXMLWithData Exception!!!");
        }
    }

Here is the crash log:
0   libicucore.A.dylib              0x34c00578 ucnv_close + 28
1   libxml2.2.dylib                 0x342a81ba xmlCharEncCloseFunc + 30
2   libxml2.2.dylib                 0x342c62d0 xmlFreeParserInputBuffer + 28
3   libxml2.2.dylib                 0x342aacbc xmlFreeInputStream + 108
4   libxml2.2.dylib                 0x342aace4 xmlFreeParserCtxt + 12
5   Foundation                      0x37d14b22 -[NSXMLParser dealloc] + 158

Please help me to resolve this issue.

sanjay
  • 11
  • 2
  • Are you running this in a subclassed NSOperation? Am I seeing similar problems. Will let you know what I discover. – Ben Dyer Aug 17 '12 at 20:42
  • Interesting comment here http://www.cocoanetics.com/2012/02/podcast-26-ios-dev-weekly/ “NSXMLParser is, in general, thread safe. The only gotcha is that, prior to this fix, it didn’t initialise the underlying libxml2 parser properly. If you’re using NSXMLParser from multiple threads, and there’s a chance that two threads might simultaneously start up parsers, you should work around this problem by initialising the libxml2 parser from the main thread, prior to using NSXMLParser at all. The routine to call is xmlInitParser. This problem was addressed in Mac OS X 10.7 and iOS 5 – Ben Dyer Aug 20 '12 at 08:45

2 Answers2

1

You are releasing parser and setting its delegate nil just after the parse method. the parsing is also taking place in background internally so you have to wait for parsing get end.

You can set Some notification or call method for releasing the parser in your parserDidEndDocument method.

Hope this helps.

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
  • Hi Kapil, i checked after calling parserDidEndDocument callback, parse method returns then i am trying to release NSXMLParser Object release. – sanjay Aug 14 '12 at 12:03
  • Please help me. Anybody has faced this issue. – sanjay Aug 14 '12 at 16:31
  • Parsing does not take place in the background. See this related SO question : http://stackoverflow.com/questions/2862911/is-nsxmlparsers-parse-method-asynchronous – Ben Dyer Aug 17 '12 at 13:51
1

I had a similar problem and it appears to be because I was mixing NSXMLParser along with other code accessing libxml - a SOAP client generated with wsdl2objc. Both were in the same thread, but if the lifetime of one overlapped the other, I got exactly the same crash and stack trace as you. By carefully controlling the lifetime of the two variants, it fixed my problem. I believe the problem I was experiencing was due to the different way xmlInitParser was being called by NSXMLParser vs the SOAP client code, although I have not verified that.

Also worth mentioning that I'm using ARC, (although I opted to shift my NSXMLParser containing class to non-ARC mainly assist with debugging) - base SDK 5.1 / 6 with deployment target of 4.0

On a side note, there's an excellent article here by Colin-C about building a thread safe version of libxml.

Hope that helps you or someone else stumbling across this.

Ben Dyer
  • 1,536
  • 11
  • 7