DerekH
DerekH

Reputation: 860

Memory Leak with NSXMLParser on IPhone

below is my code, Leaks says I am getting a memory leak around NSMutableString alloc method. I am sure it is something I simply overlooked, let me know if anyone has any thoughts. Thanks!


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if (!currentValue) {
        currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
    }

    [currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if([elementName isEqualToString:@"phone"]){

      currentAgent.phone = currentValue;
    }

    [currentValue release];

    currentValue = nil;

}

-Agent is a custom object that was created when the class was initialized. The XML is valid and has all the appropriate begin/end tags.

Upvotes: 0

Views: 283

Answers (1)

Steven Fisher
Steven Fisher

Reputation: 44866

Looking over this code, I think it's more likely that your Agent class is leaking phone. Assuming Agent uses retain for the phone property, this will cause the phone to persist longer than it should.

The creator of the object gets "credited" with the leak, even if the extra retain is somewhere else.

In other words, in Agent:

- (void)dealloc {
    self.phone = nil;
    // anything else you need to do
    [super dealloc];
}

Upvotes: 1

Related Questions