Yaniv Efraim
Yaniv Efraim

Reputation: 6713

Memory leak using class member

I use a class member called "soapResults" when connecting to a webservice. I use a parser to parse the xml results (it is a json result inside the web service).

- (void) parser:(NSXMLParser *) parser
         didStartElement:(NSString *) elementName
         namespaceURI:(NSString *) namespaceURI
         qualifiedName:(NSString *) qName
         attributes:(NSDictionary *) attributeDict {
NSString *attName = [[NSString alloc]initWithFormat:@"%@Result",methodName];
if ([elementName isEqualToString:attName]) {        
    if (!soapResults) {         
        soapResults = [[NSMutableString alloc] init];   
    }       
    elementFound = YES;     
}

[attName release];
}   

Now soapResults is a retain member and released in dealloc. I tried to release this in the connection fail/pass but did not succeed. I also tried not to alloc it at all but then I get empty results.... Any help would be appriciated

Edit: I also get memory leaks inside the parser:

-(void)parser:(NSXMLParser *) parser 
foundCharacters:(NSString *)string {    
    if (elementFound) {
        [soapResults appendString: string];//Memory leak here
    }
}

Upvotes: 0

Views: 90

Answers (2)

jbat100
jbat100

Reputation: 16827

If soapResults is a retain property you should change

soapResults = [[NSMutableString alloc] init];  

To

self.soapResults = [NSMutableString string]; 

This releases the old value and retains the new one, avoiding leaks.

Upvotes: 2

mja
mja

Reputation: 5066

You alloc soapResults = [[NSMutableString alloc] init]; here but i don't see any release.

Upvotes: 0

Related Questions