iphone - DDXML 解析器中的内存泄漏

标签 iphone objective-c ios xml-parsing nsautoreleasepool

我从 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 解析器出乎意料?

最佳答案

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

无需同时drainrelease 池;只是 drain 它。

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

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

关于iphone - DDXML 解析器中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12313724/

相关文章:

ios - 登录屏幕和导航 Controller

ios - 具有透明单元格的 UITableView 在重新加载数据时与旧单元格重叠

iphone - UIView autoresizingMask问题

ios - firebase - 获取成员的所有聊天室

ios - 为什么我会收到指针错误?

iphone - 从 IBAction 方法调用时听不到 AudioServicesPlaySystemSound 声音

ios - Interface Builder 文件中的未知类 ****

objective-c - 是否可以从 Mac 系统设置中获取代理设置用户名和密码?

ios - 使用 Flurry Analytics 记录在选项卡中花费的时间

ios - 如何从 UITextField 向 UITableView 添加多个数据? ( swift 4)