开发者

NSXMLParser Memory Leak from Second Parse Call

开发者 https://www.devze.com 2023-02-13 05:22 出处:网络
Update: While the Leaks instrument points to the first call to [parser parse];, I have identified that the leak only occurs when I make this call a second time.I don\'t see anything in the NSXMLParser

Update: While the Leaks instrument points to the first call to [parser parse];, I have identified that the leak only occurs when I make this call a second time. I don't see anything in the NSXMLParser class reference or documentation which indicates that one can't re-parse. Am I missing something? Should I create a new NSXMLParser for each time that I wish to parse the document?

I am currently trying to track down a leak in my xml parsing code for an iPhone app. I've searched through other posts on leaks with NSXMLParser, but haven't found any answers that I could use, so I would really appreciate help.

The instruments Leaks tool is detecting a leak in the following code:

NSData* data = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"collisionGraph" ofType: @"graphml"]];
NSXMLParser* parser = [[[NSXMLParser alloc] initWithData: data] autorelease];
[parser setDelegate:self];
[parser parse];    <<<<< leak here according to Leaks
self.currentPass++;

...

[parser parse];
self.currentPass++;

I removed all of the code from my delegate callback methods, but this didn't stop the leak.

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(开发者_如何学GoNSString *)qualifiedName 
   attributes:(NSDictionary *)attributeDict
{
}

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

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

Here is the view in Instruments http://i22.photobucket.com/albums/b311/Erithil/ParserLeak.png (linked because I don't have the rep to post images).

I'm really stumped by this, so any suggestions are appreciated. Thanks in advance.


It could be a leak within NSXMLParser (if e.g. xmlCleanupParser() is not called on the underlying libxml) or reusable memory mistakenly flagged as a leak. I think it's what typically happens with parsers due to them reusing (and not properly releasing?) their allocated memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消