How can I release this NSXMLParser without crashing my app?

Posted by prendio2 on Stack Overflow See other posts from Stack Overflow or by prendio2
Published on 2010-04-21T03:21:33Z Indexed on 2010/04/21 3:23 UTC
Read the original article Hit count: 283

Below is the @interface for an MREntitiesConverter object I use to strip all html tags from a string using an NSXMLParser.

@interface MREntitiesConverter : NSObject {
    NSMutableString* resultString;
    NSString* xmlStr;
    NSData *data;
    NSXMLParser* xmlParser;
}
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString*)convertEntitiesInString:(NSString*)s;
@end

And this is the implementation:

@implementation MREntitiesConverter
@synthesize resultString;
- (id)init
{
    if([super init]) {
        self.resultString = [NSMutableString string];
    }
    return self;
}

- (NSString*)convertEntitiesInString:(NSString*)s {
    xmlStr = [NSString stringWithFormat:@"<data>%@</data>", s];
    data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    xmlParser = [[NSXMLParser alloc] initWithData:data];

    [xmlParser setDelegate:self];
    [xmlParser parse];

    return [resultString autorelease];
}

- (void)dealloc {
    [data release];
    //I want to release xmlParser here but it crashes the app
    [super dealloc];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
    [self.resultString appendString:s];
}
@end

If I release xmlParser in the dealloc method I am crashing my app but without releasing I am quite obviously leaking memory.

I am new to Instruments and trying to get the hang of optimising this app. Any help you can offer on this particular issue will likely help me solve other memory issues in my app.

Yours in frustrated anticipation: ) Oisin

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c