簡體   English   中英

NSMutableDictionary中的內存泄漏

[英]Memory leaks in NSMutableDictionary

我的編碼包含內存泄漏,但我無法找到泄漏。

泄漏指向我創建“ ReportDetailItems”的方向

例如areaContainer = [[[[ReportDetailItem alloc] init] autorelease];

我已經看了幾個小時了,我全然不知所措,報告泄漏的對象是“ ReportDetailItem”,這些對象中包含的NSMutableDictionary。

請指教。

------ [ReportDetailItem.h

@interface ReportDetailItem : NSObject
{
    NSNumber *total;
    NSMutableDictionary *items;

}

@property (nonatomic, retain) NSNumber *total;
@property (nonatomic, retain) NSMutableDictionary *items;

- (NSString *)description;

@end

------ [ReportDetailItem.m

@synthesize items, total;

- (id)init {
    if (self = [super init]) {
        self.items = [NSMutableDictionary dictionaryWithCapacity:0];
        DLog("Alloc: %d", [items retainCount]);
    }   


    return self;
}

- (NSString *)description {
    return @"ReportDetailItem";
}

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

- (void)dealloc {
    [self.items release];
    [self.total release];

    items = nil;
    total = nil;

    [super dealloc];
}

@end

------ [泄漏代碼

NSError *error;
NSArray *data = [self.managedObjectContext executeFetchRequest:request error:&error];
if (data == nil || [data count] == 0) {
    DLog(@"No data.")
} else {
    for (int i=0; i < [data count]; i++) {
        TaskEntity *task = [data objectAtIndex:i];

        NSString *areaKey = task.activity.project.area.title.text;
        NSString *projectKey = task.activity.project.title.text;
        NSString *activityKey = task.activity.title.text;


        ReportDetailItem *areaContainer;
        if (![dataSource objectForKey:areaKey]) {
            areaContainer = [[[ReportDetailItem alloc] init] autorelease];
        } else { 
            areaContainer = [dataSource objectForKey:areaKey];
        }
        areaContainer.total = [NSNumber numberWithInt:([task.seconds intValue] + [areaContainer.total intValue])];
        [dataSource setObject:areaContainer forKey:areaKey];


        ReportDetailItem *projectContainer;
        if (![areaContainer.items objectForKey:projectKey]) {
            projectContainer = [[[ReportDetailItem alloc] init] autorelease];
        } else {
            projectContainer = [areaContainer.items objectForKey:projectKey];
        }
        projectContainer.total = [NSNumber numberWithInt:([task.seconds intValue] + [projectContainer.total intValue])];
        [areaContainer.items setObject:projectContainer forKey:projectKey];


        ReportDetailItem *activityContainer;
        if (![projectContainer.items objectForKey:activityKey]) {
            activityContainer = [[[ReportDetailItem alloc] init] autorelease];
        } else {
            activityContainer = [projectContainer.items objectForKey:activityKey];
        }
        activityContainer.total = [NSNumber numberWithInt:([task.seconds intValue] + [activityContainer.total intValue])];
        [projectContainer.items setObject:activityContainer forKey:activityKey];

    }
}

我對您將指針分配給ReportDetailItem的兩種方式持懷疑態度。 為什么首先要嘗試自動釋放對象? 如果沒有嘗試

ReportDetailItem *projectContainer;
        if (![areaContainer.items objectForKey:projectKey]) {
            projectContainer = [[ReportDetailItem alloc] init];  
       } else {
            projectContainer = [[areaContainer.items objectForKey:projectKey] retain];
        }
        projectContainer.total = [NSNumber numberWithInt:([task.seconds intValue] + [projectContainer.total intValue])];
        [areaContainer.items setObject:projectContainer forKey:projectKey];

      if(projectContainer) {
       [projectContainer release];
       projectContainer = nil;
      }

我找到了,泄漏位於我分配“ dataSource”的方式中

- -[泄漏


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [[NSMutableDictionary alloc] init];
    [self fetchData];
}

--- [無泄漏


- (void)viewDidLoad {
    [super viewDidLoad];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
self.dataSource = dict;
[dict release];

[self fetchData];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM