【问题标题】:Memory leak in DDXML parserDDXML 解析器中的内存泄漏
【发布时间】:2012-09-07 07:33:23
【问题描述】:

我从 Internet 加载数据,并在另一个线程中使用 DDXML 解析器对其进行解析。下面是代码(回调connectionDidFinishLoading:在后台线程中,我在后台线程中安排了URLConnection):

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connection did finish load");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    DDXMLDocument *xdoc = [[DDXMLDocument alloc] initWithData: receivedXmlData options:0 error: NULL];
    NSLog(@"document retainCount1 = %d", [xdoc retainCount]);
    NSArray *nodes = [xdoc selectNodes: @"./items/item"];
    NSLog(@"document retainCount2 = %d", [xdoc retainCount]);
    for (DDXMLElement *tabXmlItem in nodes)
    {
        // here is the parsing
    }
    NSLog(@"document retainCount3 = %d", [xdoc retainCount]);
    [xdoc release];
    [receivedXmlData setLength:0];
    [pool drain];
    [pool release];
}

我在内存分配器中看到:DDXMLDocuments、DDXMLNodes、DDXMLElements 在解析结束后仍然存在。因此,内存中有大量的 CFString 和 CFData。为什么这些对象没有被清除?也许,我错误地使用了自动释放池或 DDXML 解析器感到惊讶?

【问题讨论】:

  • 在你释放 xdoc 之后,尝试释放 'nodes' 看看你是否会崩溃。如果不是,那么可能 DDXML 正在返回一个非自动释放的数组(给定返回它的方法的名称肯定会出错)。如果这是真的,那么可能数组中的每个项目也没有自动释放。
  • 看来是自动发布的问题。有趣的是,当我将 Internet 连接从后台转移到前台线程并且只在后台进行解析时,自动释放池就可以工作了。

标签: iphone objective-c ios xml-parsing nsautoreleasepool


【解决方案1】:

retainCount 没用。不要叫它。 http://whentouseretaincount.com/

不需要drainrelease 池;就drain吧。

更好的是,在池的范围内使用@autoreleasepool {...}

由于您使用的是分配工具,因此请打开“跟踪引用计数”。然后,您可以查看保留/释放对象的历史记录,看看它们为什么仍然存在。

【讨论】:

  • 感谢您的回答:) 我解决了这个问题,但我还不明白如何。有一个很大的研究领域。
猜你喜欢
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 2011-10-18
  • 2010-11-04
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多