Re: Memory Management for an Array
Re: Memory Management for an Array
- Subject: Re: Memory Management for an Array
- From: Alexander Spohr <email@hidden>
- Date: Mon, 13 Jun 2011 21:31:40 +0200
The shown memory handling is wrong.
Am 13.06.2011 um 16:31 schrieb Bing Li:
> + (NSString *)read:(NSString *)xml Path:(NSString *)xPath
This methods signature should be read:path: with a lowercase p.
> {
> // The Instruments indicated the following line got memory leaks.
> The amount of leaked memory was not large (about 400KB)
400 KB is a VERY large amount to leak.
> NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:xml
> options:NSXMLDocumentTidyXML error:NULL];
>
> NSArray *nodes = [xmlDoc nodesForXPath:xPath error:NULL];
You hold a pointer to nodes but you do not retain them (usually no problem)
> [xmlDoc release];
But here you release xmlDoc which was the only owner of nodes.
After this line nodes might be gone.
Use autorelease instead. That will keep xmlDoc alive long enough.
> if ([nodes count] > 0)
This might kill you.
> {
> return [[nodes objectAtIndex:0] stringValue];
This as well.
> }
> else
> {
> return @"";
> }
> }
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden