简体   繁体   English

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

[英]Why is this code leaking so much?

Can someone please tell me why this code is leaking? 有人可以告诉我为什么这个代码泄漏了吗? Instruments is showing several leaks and i cant figure out why and i have gone through the code several times? 仪器显示几个泄漏,我无法弄清楚为什么和我已经多次通过代码?

Basically the first method initializes a bunch of Thumbnail classes from a property list and then builds an array which is used as a tableviews datasource. 基本上,第一个方法从属性列表初始化一堆Thumbnail类,然后构建一个用作tableviews数据源的数组。 The second method updates the tableviews datasource when a new section is chosen: 第二种方法在选择新部分时更新tableviews数据源:

Here is the initialization and dealloc calls: 这是初始化和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;

And here are the two methods causing the leaks: 以下是导致泄漏的两种方法:

- (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;
    }
}

tempThumbsArray此行之后,不会释放tempThumbsArray变量changeDatasourceToSection:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM