0

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

4

2 回答 2

0

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

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

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

于 2012-08-23T04:55:31.960 回答
0

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

于 2012-05-08T07:50:08.757 回答