NSMutableString *xmlStr = [NSMutableString stringWithCapacity:100];
[xmlStr appendString:@"<?xml version=\"1.0\"?>"];
[xmlStr appendString:@"<request>"];
[xmlStr appendString:@" <category>"];
[xmlStr appendString:@" </category>"];
[xmlStr appendString:@"</request>"];
NSXMLDocument *nsDoc = [[NSXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
NSXMLNode *nsChild1 = [[nsDoc rootElement] childAtIndex:0];
NSXMLNode *nsChild2 = [[nsDoc rootElement] childAtIndex:0];
if (nsChild1 == nsChild2) {
NSLog(@"Same request gives same Pointer, the Node is not allocated twice");
} else {
NSLog(@"Same request gives different Pointer, the Node is allocated twice wasting time and memory");
}
[nsDoc release];
DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
DDXMLNode *ddChild1 = [[ddDoc rootElement] childAtIndex:0];
DDXMLNode *ddChild2 = [[ddDoc rootElement] childAtIndex:0];
if (ddChild1 == ddChild2) {
NSLog(@"Same request gives same Pointer, the Node is not allocated twice");
} else {
NSLog(@"Same request gives different Pointer, the Node is allocated twice wasting time and memory");
}
[ddDoc release];
I would suggest to store only one pointer to an DDXMLNode in _private. When a Node is requested, look up _private, if NULL instanciate a new one and store the pointer in _private, if non-NULL call autorelease on the stored instance and hand it back. On deallocation you would set _private back to NULL and go on with checking the parent pointer releasing underlying libxml structures if appropriate.
This way you could easily trash the hole _xmlRetain linked List.
Please tell me if I overlooked some deeper implications that could prevent using that kind of easier, lazier memory management.