gpt4 book ai didi

cocoa - NSMutableDictionary 内存问题,导致 NSCFDictionary 内存泄漏

转载 作者:行者123 更新时间:2023-12-03 17:00:20 31 4
gpt4 key购买 nike

请帮我解决以下问题:

- (NSDictionary *)getGamesList
{
NSMutableDictionary *gamesDictionary = [[NSMutableDictionary dictionary] retain];
// I was trying to change this on the commented code below, but did have no effect
// NSMutableDictionary *gamesDictionary = [[NSMutableDictionary alloc] init];
// [gamesDictionary retain];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *key = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSArray *gameDate = [key componentsSeparatedByString:@" "];
NSNumber *_id = [[NSNumber alloc] initWithInt:sqlite3_column_int(statement, 0)];
NSString *date_time = [NSString stringWithFormat:@"%@, %@",[gameDate objectAtIndex:0],[gameDate objectAtIndex:2]];
if (![gamesDictionary valueForKey:date_time]) [gamesDictionary setValue:[NSMutableArray array] forKey:date_time];
[[gamesDictionary valueForKey:date_time] addObject:[[_id copy] autorelease]];
[_id release];
}
sqlite3_reset(statement);
return gamesDictionary;
}

泄漏从另一个类的另一个方法开始,在那里调用 getGamesList 方法,如下所示:

NSMutableDictionary *gamesDictionary;
gamesDictionary = [[NSMutableDictionary dictionaryWithDictionary:[appDelegate getGamesList]] retain];

之后,字符串中有很多指向 NSCFArray 的泄漏:

NSArray *keys = [[NSArray arrayWithArray:[gamesDictionary allKeys]] retain]; 

在此方法中:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *keys = [[NSArray arrayWithArray:[gamesDictionary allKeys]] retain];
if ([keys count] != 0) return [[keys objectAtIndex:section] uppercaseString];
return @"";
}

我假设这些东西是相互关联的,但我仍然无法理解所有的内存管理技巧。非常感谢!

最佳答案

多年来没有使用 Cocoa(这就是为什么我不能告诉你确切的答案:/)。但我猜你的问题是你系统地使用 retain在你的物体上。

由于对象引用计数永远不会达到 0,因此所有字典都保留在内存中并且不会释放。

尝试删除retain[NSArray arrayWithArray][NSMutableDictionary dictionaryWithDictionary

http://en.wikibooks.org/wiki/Programming_Mac_OS_X_with_Cocoa_for_beginners/Some_Cocoa_essential_principles#Retain_and_Release

关于cocoa - NSMutableDictionary 内存问题,导致 NSCFDictionary 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2391903/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com