0

I am using xml parser in my application. When i run my application for 10 to 15 time its works fine but suddenly its giving me the bad_access with above codes. My xml parser code as follow:

    -(BOOL)getTheServerStatus:(NSData *)webData
{
    if (webData==NULL) 
    {
        return FALSE;
    }

    parser=[[NSXMLParser alloc]initWithData:webData];
    [parser setDelegate:self];

    [self performSelectorOnMainThread:@selector(parseData:)
                           withObject:webData
                        waitUntilDone:YES];


    if([strVal isEqualToString:@"ok"])
    {
        return TRUE;   
    }

    else 
    {
        return FALSE;

    }

}

- (void)parseData:(NSData *)webData
{
    if(webData==NULL)
    {
        NSLog(@"web data is NULL");
    }
    [parser parse];
}

I am using automatic reference counting .So what is the issue with my code?

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79

1 Answers1

0

I assume that getTheServerStatus is getting called on a thread which is NOT the main thread. Yet you do the parsing on the main thread. Is it possible a second thread us clobbering the parser?

EDIT: code changed

So what you should do is NOT block in getTheServerStatus:, but break your problem into two parts. The first is you want to get the status - so you are going to dispatch a block to do that work for you. While that is going on you can throw up a spinner, or just disable some of your UI. That is a design decision of course. When the background thread looking for status is done, it will message you back on the main thread with the result, and you can take whatever action you want then. I just posted an answer to a similar question that has even more code, which you might find helpful.

{ // ivars
    NSXMLParser *parser; // so you can send it abortParsing to cancel the background block
}

-(void)getTheServerStatus:(NSData *)webData
{
    if (webData==nil) // nil is for objects, NULL for pointers
    {
        dispatch_async(dispatch_get_main_queue(), ^{ [self parseResult:NO]; } );
    }

    parser=[[NSXMLParser alloc]initWithData:webData];
    [parser setDelegate:self];

    dispatch_async(dispatch_get_global_queue(0,0), ^
        {
            BOOL ret = [parser parse];
            parser = nil;
            if(ret == YES) {
                    ret = [strVal isEqualToString:@"ok"]; // EDIT
            }
            dispatch_async(dispatch_get_main_queue(), ^{ [self parseResult:ret]; } );
        } );
}

-(void)parserResult:(BOOL)retCode
{
    // now on main thread...
    if(retCode == YES) ....
    else .....
}
Community
  • 1
  • 1
David H
  • 40,852
  • 12
  • 92
  • 138
  • But i don't want to return the success code return from the parser.I as parsing the web response which is having the status as its one of the attribute. I want to return that status thats why i am checking if([strVal isEqualToString:@"ok"]) – V-Xtreme Aug 18 '12 at 06:18