iPhone:自动释放对象上的内存泄漏?

3

我正在使用XMLParser类,其中包含一个带有XMLElement对象的数组。 使用autorelease操作来分配XMLElement。 但是,由于某种原因,在此行上(使用工具Instruments),我遇到了内存泄漏:

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
5个回答

3

在XMLParser中,您必须在dealloc方法中释放currentElement

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

更新:当您完成后,应该执行self.currentElement = nil;而不是currentElement = nil;


2
这是:
self.currentElement = [[[XMLElement alloc] init] autorelease];

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

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

currentElement = nil;

这导致泄漏,因为你没有释放currentElement


谢谢你的回答。我按照你的建议进行了更新,但是我仍然遇到了内存泄漏问题。以下是更新后的代码: - user480451
  • (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ //NSLog(@"Processing didEndElement: %@", elementName); if(currentElement == nil){ if( currentParentElement.parentElement){ self.currentParentElement = currentParentElement.parentElement; } }else{ currentElement.textValue = currentElementValue; [currentElementValue release]; self.currentElementValue = nil; self.currentParentElement = currentElement.parentElement; self.currentElement = nil; } }
- user480451
@user480451:你真的希望有人能够读懂那个吗?编辑问题并将代码放入其中。 - JeremyP
没错,你说得对。我在这里没有找到“回复”按钮...所以我会把它作为一个答案添加。 - user480451

0

大家好。

原因只有一个简单的事情,

而不是

@property(nonatomic, retain) XMLElement *parentElement;

必须是

@property(nonatomic, assign) XMLElement *parentElement;

同时,您还需要从dealloc中删除[parentElement release]

原因是您正在创建循环引用。父级到子级和子级到父级

当您释放解析器对象时,元素仍然以指针计数> 0的形式出现在内存中。


0

从我在这个页面上看到的内容来看,这是一个已记录的苹果bug。我在我的一些应用程序中也遇到了同样的问题...


0
你的 XMLParser 是否在后台线程上运行?如果是,你需要为该线程创建一个 NSAutoReleasePool,以便使你的 [[[XMLElement alloc] init] autorelease] 调用起作用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接