views:

57

answers:

2

I've parsed an xml and stored the parsed data on strings which are property declared, retained and synthesized, the values are getting into these strings during parsing, but are being invalidated after the parser finish parsing. I'm not able to access them from another IBAction method, where i get EXEC BAD ACCESS error, which, from my experiences, think that arises when the debugger is accessing an invalidated memory.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"resp"]) {
    // clear out our story item caches...
}
else if ([currentElement isEqualToString:@"org"]) {
    name = [attributeDict objectForKey:@"fileid"];
    extention = [attributeDict objectForKey:@"extension"];
    NSLog(@"image named %@ of type %@",name,extention);
}
else if ([currentElement isEqualToString:@"photo"]) {
    photoid = [attributeDict objectForKey:@"photoid"];
    NSLog (@"photo id = %@",photoid);
}       

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...
    if ([currentElement isEqualToString:@"userid"]) {
        userid =[NSString stringWithString:string];
        NSLog (@"user id = %@",userid);
        } 
    else if ([currentElement isEqualToString:@"albumid"]) {
        albumid =[NSString stringWithString:string];
        NSLog (@"album id = %@",albumid);
    }
}

This portion is working fine, but

- (IBAction)download {
    NSString *urlstr = [NSString stringWithFormat:@"http://staging-data.mysite.com/%@/albumphoto/%@/%@/%@.%@",userid,albumid,photoid,name,extention];
    NSLog(urlstr);
    NSURL *url= [NSURL URLWithString:urlstr];
    NSData *imgdata = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:imgdata];
    newImage.image=img;
    [imgdata release];

}

This results in error. pls help

A: 

You have to implement this method also to know when an end tag is encountered.

  • (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
raaz
is that compulsary, i've included that one,but there's nothing inside it, an empty one.
Nithin
yes ,this is a delegate method which will inform the parser that it moves to the end of the element.And it shouldn't be empty If you encounter the an element, you have to add the object to any array.
raaz
Implement this delegate method also to know errors encountered by the parser.- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
raaz
i've used them all, but didn't shown here as it is not relevant,
Nithin
+1  A: 

I suggest you look carefully at this line:

name = [attributeDict objectForKey:@"fileid"];

Which sets an attribute to an NSString owned by the XML parser without copying it, retaining it etc. I suggest you take a copy, and retain that.

My concern is that when the XML parser goes away, it takes the name string with it, along with extension and photoid.

Alex Brown
retaining is what required, thnx
Nithin
Don't forget to release it when needed!
Adam Woś