使用类成员的内存泄漏

时间:2011-10-05 13:40:44

标签: iphone objective-c web-services memory-leaks

在连接到webservice时,我使用名为“soapResults”的类成员。我使用解析器来解析xml结果(它是Web服务中的json结果)。

- (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];
}   

现在soapResults是一个保留成员并以dealloc发布。我尝试在连接失败/通过时释放它但没有成功。我也尝试过根本不分配它,但后来我得到空的结果......任何帮助都会被扼杀

编辑: 我也在解析器中出现内存泄漏:

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

2 个答案:

答案 0 :(得分:2)

如果soapResults是一个保留属性,则应更改

soapResults = [[NSMutableString alloc] init];  

self.soapResults = [NSMutableString string]; 

这会释放旧值并保留新值,避免泄漏。

答案 1 :(得分:0)

您在这里分配soapResults = [[NSMutableString alloc] init];,但我没有看到任何版本。