I’m using the following function to put each “facility” node of my XML in a NSMutable array:
-(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];
}
Note that the pool is necessary, ’cause i call the grabXML method in a separate thread.
Using instruments i can see that the following line generates a leak
[content addObject:[eventNode copy]];
And if i change it to
[content addObject:eventNode];
i’m not able to access the XCMLNode later (it seems to be null).
I can avoid the leak putting this on my dealloc method:
for (CXMLNode *node in content) {
[node release];
}
But i think i’m doing something wrong… or at least i’m not aware of what’s going on… Please can you give me a clue?
Thanks!
copycreates an object with a retain count of 1,-addObject:adds an additional retain, so you have to either releaseeventNodeafter adding it to the array or autorelease the copy: