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