我从 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/
无需同时drain
和release
池;只是 drain
它。
更好的是,在池的范围内使用 @autoreleasepool {...}
。
由于您使用的是分配工具,因此请打开“跟踪引用计数”。然后,您可以查看保留/释放对象的历史记录,看看为什么它们仍然存在。
关于iphone - DDXML 解析器中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12313724/