为什么这段代码泄漏了这么多?

时间:2011-04-01 18:36:13

标签: iphone objective-c memory-leaks

有人可以告诉我为什么这段代码泄漏了吗?仪器显示几个泄漏,我无法弄清楚为什么和我已经多次通过代码?

基本上,第一个方法从属性列表初始化一堆Thumbnail类,然后构建一个用作tableviews数据源的数组。第二种方法在选择新部分时更新tableviews数据源:

这是初始化和dealloc调用:

NSMutableArray *thumbnailsDatasource;   
NSMutableDictionary *sectionsDictionary;

@property (nonatomic, retain) NSMutableArray *thumbnailsDatasource;
@property (nonatomic, retain) NSMutableDictionary *sectionsDictionary;
@synthesize thumbnailsDatasource;
@synthesize sectionsDictionary;

[thumbnailsDatasource release], thumbnailsDatasource = nil; 
[sectionsDictionary release], sectionsDictionary = nil;

以下是导致泄漏的两种方法:

- (void)initThumbnails
{
    // init sections dictionary
    NSMutableDictionary *sectionsDict = [[NSMutableDictionary alloc] init];

    // read the cover data from the plist
    NSString *thePath = [[NSBundle mainBundle]  pathForResource:kThumbnailPlistName ofType:@"plist"];
    NSArray *thumbsArray = [[NSArray alloc] initWithContentsOfFile:thePath];

    // iterate over the values in the dictionary
    for (NSDictionary *currentThumb in thumbsArray)
    {
        Thumbnail *newThumb = [[Thumbnail alloc] initWithName:[currentThumb objectForKey:kThumbnailName]
                                                      fileURL:[currentThumb objectForKey:kThumbnailFileURL]
                                                     imageURL:[currentThumb objectForKey:kThumbnailImageURL]
                                                      section:[currentThumb objectForKey:kThumbnailSection]
                                                   pageNumber:[(NSNumber*)[currentThumb objectForKey:kThumbnailPageNumber] integerValue]];

        // check if we already have an array in sectionsDictionary for that section
        if ([sectionsDict objectForKey:newThumb.section]) 
        {       
            NSMutableArray *currentSectionArray = [sectionsDict objectForKey:newThumb.section];
            [currentSectionArray addObject:newThumb];
        }
        else 
        {
            NSMutableArray *newSectionArray = [[NSMutableArray alloc] init];
            [newSectionArray addObject:newThumb];

            [sectionsDict setObject:newSectionArray forKey:newThumb.section];
            [newSectionArray release], newSectionArray = nil;
        }       
        [newThumb release], newThumb = nil;
    }

    [thumbsArray release], thumbsArray = nil;

    self.sectionsDictionary = sectionsDict;
    [sectionsDict release], sectionsDict = nil;


    NSMutableArray *tempThumbsArray = [[NSMutableArray alloc] init];

    // set datasource to All sections in dictionary
    NSArray *allValuesArray = [self.sectionsDictionary allValues];

    for (NSMutableArray *currentSection in allValuesArray)
    {
        [tempThumbsArray addObjectsFromArray:currentSection];
    }

    // sort array by pageNumber asc
    [tempThumbsArray sortUsingSelector:@selector(comparePageNumberAsc:)];
    self.thumbnailsDatasource = tempThumbsArray;

    [tempThumbsArray release], tempThumbsArray = nil;

}

- (void)changeDatasourceToSection:(NSString*)section
{

    if ([section isEqualToString:kAllSectionsName]) {

        NSMutableArray *tempThumbsArray = [[NSMutableArray alloc] init];

        // set datasource to All sections in dictionary
        NSArray *allValuesArray = [self.sectionsDictionary allValues];

        for (NSMutableArray *currentSection in allValuesArray)
        {
            [tempThumbsArray addObjectsFromArray:currentSection];
        }

        // sort array by pageNumber asc
        [tempThumbsArray sortUsingSelector:@selector(comparePageNumberAsc:)];
        self.thumbnailsDatasource = tempThumbsArray;

    }
    else {
        NSMutableArray *newDatasource = (NSMutableArray*)[self.sectionsDictionary objectForKey:section];
        [newDatasource sortUsingSelector:@selector(comparePageNumberAsc:)];
        self.thumbnailsDatasource = newDatasource;
    }
}

1 个答案:

答案 0 :(得分:2)

tempThumbsArray

中此行之后未发布changeDatasourceToSection:变量
NSMutableArray *tempThumbsArray = [[NSMutableArray alloc] init];