我使用下面的函數來把我的XML的每個「設施」節點在NSMutable陣列:自動釋放池和內存管理
-(void) grabXML {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:data options:0 error:nil] autorelease];
NSArray *nodes = [[doc rootElement] nodesForXPath:@"//facilities" error:nil];
for (CXMLNode *itemNode in nodes)
{
for (CXMLNode *eventNode in [itemNode children])
{
if ([[eventNode name] isEqualToString:@"facility"]) {
[content addObject:[eventNode copy]];
}
}
}
loading = FALSE;
[table reloadData];
[pool release];
}
注意,游泳池是必要的,因爲我叫grabXML方法在一個單獨的線程中。
使用儀器我可以看到下面的行產生泄漏
[content addObject:[eventNode copy]];
如果我將其更改爲
[content addObject:eventNode];
我不能稍後訪問XCMLNode(它似乎爲空)。
我能避免泄露把這個在我的dealloc方法:
for (CXMLNode *node in content) {
[node release];
}
但我覺得我做錯了什麼......或者至少我不知道發生了什麼事情的...請你能給我一點線索嗎?
謝謝!