3

我正在使用 XMLParser 类,它包含一个带有 XMLElement 对象的数组。正在使用自动释放操作分配 XMLElement。但是,由于某种原因,我在这条线上遇到了内存泄漏(使用仪器):

self.currentElement = [[[XMLElement alloc] init] autorelease];

我还在调用者类中发布了 XMLParser 对象。我在下面的 XMLParser 源代码中突出显示了“有问题的”行作为注释。

我真的尝试了一切来修复它,但不幸的是我不明白为什么会发生这种情况。任何帮助表示赞赏!

非常感谢!

// --- XMLElement

#import <Foundation/Foundation.h>


@interface XMLElement : NSObject {
     NSDictionary *attributes;
     NSMutableArray *children;
     NSString *textValue;
     NSString *tagName;
     XMLElement *parentElement;
}

-(id) init;
-(void) addChild:(XMLElement*) child;

@property(nonatomic, retain) NSDictionary *attributes;
@property(nonatomic, retain) NSMutableArray *children;
@property(nonatomic, retain) NSString *textValue;
@property(nonatomic, retain) NSString *tagName;
@property(nonatomic, retain) XMLElement *parentElement;

@end


#import "XMLElement.h"


@implementation XMLElement

@synthesize attributes, children, textValue, parentElement, tagName;

-(id)init {
     if(self = [super init]) {
          children = [[NSMutableArray alloc] init];
     }
     return self;
}

-(void) addChild:(XMLElement*) child{
     [self.children addObject:child];
}

- (void)dealloc {
     [attributes release];
     [children release];
     [textValue release];
     [tagName release];
     [parentElement release];
    [super dealloc];
}

@end





// --- XMLParser

#import <Foundation/Foundation.h>
#import "XMLElement.h"

@interface XMLParser : NSObject<NSXMLParserDelegate> {
     XMLElement *currentElement;
     XMLElement *currentParentElement;
     NSMutableString *currentElementValue;
}

- (BOOL)parseData: (NSData*) dataToParse;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
     attributes:(NSDictionary *)attributeDict;

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

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

@property (nonatomic, retain) XMLElement *currentParentElement;
@property (nonatomic, retain) XMLElement *currentElement;
@property (nonatomic, retain) NSMutableString *currentElementValue;

@end


#import "XMLParser.h"


@implementation XMLParser

@synthesize currentElementValue, currentElement, currentParentElement;

- (BOOL)parseData: (NSData*) dataToParse {

     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataToParse];
     [parser setDelegate:self];
     BOOL success = [parser parse];
     [parser release];
     return success;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
     attributes:(NSDictionary *)attributeDict{
     if(currentElement){
          self.currentParentElement = currentElement;
     }


     // ------------------------------------------------------------
     // Instruments is marking this line as source of the leak with 90%
     self.currentElement = [[[XMLElement alloc] init] autorelease];
     // --------

     currentElement.tagName = elementName;
     currentElement.attributes = attributeDict;
     currentElement.parentElement = self.currentParentElement;
     if(self.currentParentElement){
          [self.currentParentElement addChild:currentElement]; // and this one with 10%
     }

     self.currentElementValue = nil;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
     if(!currentElement) {
          return;
     }

     if(currentElementValue == nil)
          self.currentElementValue = [NSMutableString stringWithString:string];
     else
          [currentElementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
     if(currentElement == nil){
          if( currentParentElement.parentElement){
               self.currentParentElement = currentParentElement.parentElement;
          }
     }else{
          currentElement.textValue = currentElementValue; 
          [currentElementValue release];
          currentElementValue = nil;
          self.currentParentElement = currentElement.parentElement;
          currentElement = nil;
     }
}

- (void)dealloc {
     [currentParentElement release];
     [currentElement release];
    [super dealloc];
}

@end
4

5 回答 5

3

您必须在 XMLParsercurrentElement中的方法中释放。dealloc

它被创建为autorelease但随后分配给一个retain属性,因此您实际上保留了一次(这很好)。

更新:self.currentElement = nil;当你完成它时你应该这样做,而不是currentElement = nil;.

于 2010-10-19T12:40:32.300 回答
2

这个:

self.currentElement = [[[XMLElement alloc] init] autorelease];

保留currentElement(因为该属性是用 声明的retain)。

稍后,在 中parser:didEndElement:namespaceURI:qualifiedName:,您执行以下操作:

currentElement = nil;

这会导致泄漏,因为您没有释放currentElement.

于 2010-10-19T12:40:48.773 回答
0

From what I see on this page, this is a documented apple bug. I have experienced the same problem in some of my apps...

于 2010-10-19T14:12:10.507 回答
0

嗨,大家好。原因很简单,

代替

@property(nonatomic, retain) XMLElement *parentElement;

一定是

@property(nonatomic, assign) XMLElement *parentElement;

[parentElement release]你也需要删除dealloc

原因是您正在创建循环引用。父母对孩子和孩子对父母

并且当您重新解析解析器对象时,元素仍然出现在内存中,指针 xount > 0

于 2010-10-28T11:11:50.497 回答
0

XMLParser是在后台线程上运行的吗?如果是这样,您需要NSAutoReleasePool为该线程创建一个以便您的[[[XMLElement alloc] init] autorelease]调用正常工作。

于 2010-10-19T14:50:46.880 回答