NSXML解析器内存泄漏

时间:2012-10-23 21:52:32

标签: objective-c cocoa-touch nsxmlparser

任何人都可以告诉我这个解析器代码是否有任何内存泄漏。

#import <Foundation/Foundation.h>

@interface WSXMLParser : NSObject<NSXMLParserDelegate>
{
    NSMutableDictionary* fullInfo;
    NSMutableString* keyName;
    NSMutableString* keyValue;
    BOOL shouldAddKeyValue;

}



- (NSMutableDictionary *)metaDataForXML:(NSURL*)xmlURL;

@end




#import "WSXMLParser.h"

@implementation WSXMLParser

-(NSMutableDictionary *)metaDataForXML:(NSURL *)xmlURL
{
    fullInfo=[[NSMutableDictionary alloc]init];
    keyValue=[[NSMutableString alloc] init];
    shouldAddKeyValue=NO;

    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [parser setDelegate:self];
    [parser parse];

    return fullInfo;
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{
   NSLog(@"parsing start");
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
   NSLog(@"parsing end");
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    [keyValue setString:@""];
    shouldAddKeyValue=YES;

    keyName=[NSMutableString stringWithString:elementName];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    [keyValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if(shouldAddKeyValue)
    {
        NSCharacterSet * whitespaceCharacterSet=[NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimmedString = [keyValue stringByTrimmingCharactersInSet: whitespaceCharacterSet];

        if ([trimmedString length]==0)                
            return;

        NSString* value=[keyValue copy];
        [fullInfo setObject:value forKey:keyName];
        [keyValue setString:@""];
    }

    shouldAddKeyValue=NO;
}

@end

我正在使用xcode 4.5.1并启用了项目ARC。这个解析代码需要每隔5-10秒调用一次,如果这个解析器被调用几分钟,为什么应用程序会耗尽内存?

0 个答案:

没有答案