开发者

cocoa; What is the leak in this code?

NSMutableArray *tempData=[[NSMutableArray alloc]init];  
    TBXMLElement * city = [TBXML childElementNamed:@"city" parentElement:root];
    while(city!=nil){
        if([TBXML valueOfAttributeNamed:@"name" forElement:city]!=nil){
        NSString *tempDataHolder=[NSString stringWithFormat :@"%@,%@",[TBXML valueOfAttribute开发者_如何学JAVANamed:@"name" forElement:city],[TBXML valueOfAttributeNamed:@"country_name" forElement:city]]; 
        [tempData addObject:[tempDataHolder copy]];         
        [tempDataHolder release];
               }
city = [TBXML nextSiblingNamed:@"city" searchFromElement:city];         

    }
    tableData=[tempData copy];
    [tableCities reloadData];
    [tempData release];

Instruments with Memory leaks says there is a leak of multiple NSCFStrings,i have been trying to figure it out for a while, any help is highly appreciated.

Thanks

edit: The above set of code runs a few times, and i have a bunch of leaks referring to NSCFString - NSPlaceholderString. I am releasing tempDataHolder almost immediately and the rest of the variables are being released as well. I cant pin point on where the leak is.


Copied objects need to be released by the owner. That is, the copy method returns a new object that has a retain count of 1. In your situation, the culprit seems to be this line:

[tempData addObject:[tempDataHolder copy]];

Containers retain their elements, but the copied object already has a retain count of 1 before being inserted in the array. The copied object is therefore leaking.

Simply adding tempDataHolder in your array (not a copy) should solve it.

Also, tempDataHolder is an auto-released object and shouldn't be released explicitly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜