【发布时间】: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