【问题标题】:NSPlaceholderString initWithFormat:locale:arguments: Xcode instruments leakNSPlaceholderString initWithFormat:locale:arguments: Xcode 仪器泄漏
【发布时间】:2012-05-07 01:51:01
【问题描述】:

以下函数; “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 文件)。任何关于如何消除泄漏的建议都会非常有帮助。

【问题讨论】:

    标签: ios json memory-leaks nsstring nsdictionary


    【解决方案1】:

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

    【讨论】:

    • 感谢哈雷的建议。不幸的是,即使在将代码移至任一方法之后,同一行仍然存在内存泄漏。以下是泄漏的详细信息:Responsible Frame - NSPlaceholderString initWithFormat:locale:arguments: Responsible Library - Foundation
    【解决方案2】:

    尝试为您的 NSMutableArrays 实现 setter/getter,例如:

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

    并且还插入一个异常断点来知道你的代码到底在哪里崩溃了。

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 2014-04-03
      • 2011-11-02
      • 2020-08-01
      • 2011-10-04
      • 2017-10-09
      • 2012-04-03
      • 2014-01-02
      相关资源
      最近更新 更多