NSPlaceholderString initWithFormat:locale:arguments:Xcode instruments leak

时间:2012-04-26 10:25:32

标签: ios json memory-leaks nsstring nsdictionary

以下功能; “ProcessArrayForscrollView”读取通过发送URL接收的JSON数据; “urlProcess”并将获取的数据值以NSString格式存储在NSMutableArrays中。

#import "SBJson.h"
#import "SBJsonStreamParser.h"

@implementation AllShowsViewController

NSURL *url = nil;
NSString * arrayDataString;
NSData *dataAllShowsView;
NSError *errorAllShowsView;
NSString *data_stringAllShowsView;
SBJsonParser *parserAllShowsView;
NSArray *data_arrayAllShowsView;
NSDictionary *itemNSDictAllShowsView;
NSMutableArray  *thumbnailImageURLAllShows;
NSMutableArray  *thumbnailShowCountAllShows;

-(void)ProcessArrayForscrollView{
    thumbnailImageURLAllShows = [[NSMutableArray alloc] init];
    thumbnailShowCountAllShows = [[NSMutableArray alloc] init];
    dataAllShowsView = [[[NSData alloc] initWithContentsOfURL:urlProcess] autorelease];
    errorAllShowsView = nil;
    data_stringAllShowsView = [[[NSString alloc] initWithData:dataAllShowsView encoding:NSUTF8StringEncoding]autorelease];
    parserAllShowsView = [[[SBJsonParser alloc] init] autorelease];
    data_arrayAllShowsView = [[[NSArray alloc] initWithArray:[parserAllShowsView objectWithString:data_stringAllShowsView error:&errorAllShowsView]] autorelease];
    for(itemNSDictAllShowsView in data_arrayAllShowsView){
         arrayDataString = [NSString stringWithFormat:@"%@",[itemNSDictAllShowsView objectForKey:@"thumbnail_small"]];    //memory leak notification here
        [thumbnailImageURLAllShows addObject: arrayDataString];  
        arrayDataString = nil;

        arrayDataString = [NSString stringWithFormat:@"%@",[itemNSDictAllShowsView objectForKey:@"showcount"]];    //memory leak notification here
        [thumbnailShowCountAllShows addObject: arrayDataString];
        arrayDataString = nil;
    }
}

-(void)dealloc{
    [super dealloc];
}

- (void)viewDidUnload{
    if(thumbnailImageURLAllShows != nil){
        [thumbnailImageURLAllShows release];
        thumbnailImageURLAllShows = nil;
    }
    if(thumbnailShowCountAllShows != nil){
        [thumbnailShowCountAllShows release];
        thumbnailShowCountAllShows = nil;
    }
    [super viewDidUnload];
}
@end

我运行代码以使用Xcode仪器检查内存泄漏并在两条线路上发生泄漏。从上面的viewController切换后通知此泄漏; “AllShowsViewController”到任何其他viewController(有一个nib文件)。关于如何删除泄漏的任何建议都会非常有帮助。

2 个答案:

答案 0 :(得分:0)

我认为viewDidUnload不能保证被调用。字符串未正确释放,因为容器(NSMutableArray s)不是。我建议您尝试将thumbnailImageURLAllShowsthumbnailShowCountAllShows的清理代码移到viewDidDisappeardealloc,看看是否会出现内存泄漏。

答案 1 :(得分:0)

尝试为你的NSMutableArrays实现你的setter / getters,如:

if (!thumbnailImageURLAllShows) {
        thumbnailImageURLAllShows = [[NSMutableArray alloc] init];
}

还插入一个异常断点,以了解您的代码在哪里崩溃。