Iphone 使用仪器进行内存泄漏检测
我正在试图找出这段代码中的内存泄漏在哪里Iphone 使用仪器进行内存泄漏检测,iphone,memory-leaks,Iphone,Memory Leaks,我正在试图找出这段代码中的内存泄漏在哪里 - (NSMutableArray *) fetchAidDetails:(NSNumber *) rowID { NSMutableArray *list = [[NSMutableArray alloc] init]; FMDatabase *db = [[FMDatabase databaseWithPath:[self dbPath]] retain]; if(![db open]) { [db r
- (NSMutableArray *) fetchAidDetails:(NSNumber *) rowID {
NSMutableArray *list = [[NSMutableArray alloc] init];
FMDatabase *db = [[FMDatabase databaseWithPath:[self dbPath]] retain];
if(![db open]) {
[db release];
return [list autorelease];
}
NSString *query = [NSString stringWithFormat:@"select legislative_type, legislative_name from legislative_aid where official_id = %d", rowID.unsignedIntValue];
FMResultSet *result = [[db executeQueryWithFormat:query] retain];
while ([result next]) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
NSString *type = [[NSString alloc] init];
type = [result stringForColumn:@"legislative_type"];
[item setObject:type forKey:@"legislative_type"];
[type release];
NSString *party = [[NSString alloc] init];
party = [result stringForColumn:@"legislative_name"];
[item setObject:party forKey:@"legislative_name"];
[party release];
[list addObject:item];
[item release];
}
[result release];
[db close];
[db release];
return [list autorelease];
}
[item retainCount]在[item release]之前给出2,[list autorelease]将使引用计数为0,我在这里是否出错
请帮忙
多谢各位
NSString *type = [[NSString alloc] init];
type = [result stringForColumn:@"legislative_type"];
您正在创建一个NSString“type”,其保留计数为1,但随后为其指定了一个不同的对象。删除第一行并仅使用以下命令:
NSString *type = [result stringForColumn:@"legislative_type"];
您还需要删除[type release]
,因为stringForColumn
返回一个自动删除的NSString(至少如果它遵守Cocoa命名约定)。- 不要依赖于
。系统或类的内部代码可以在内部保留您的对象,稍后再释放它,这是它们的业务,您不知道,因此您不知道在retainCount
中期望什么值。这不是寻找漏洞的好方法retainCount
- 在代码中多次分配一个对象(在本例中为
),然后忽略它以使用其他值覆盖变量:NSString
NSString *party = [[NSString alloc] init]; // You allocate memory for an NSString object party = [result stringForColumn:@"legislative_name"]; // then you completely forget about it and override your party variable with a different object
int i = 5;
i = 12;
当然,第一个值是无用的,不需要。但在代码中,您从未使用的这个值分配了从未回收的内存,因此它会泄漏
- 然后,在您将另一个对象发送到您的
变量后,您稍后将向该新对象发送一个参与方
。但是,释放
已经包含自动删除对象,因为您影响了它,并且根据代码命名约定,此方法返回自动删除对象。因此,您不需要在此新对象上发送参与方
释放
while ([result next])
{
@autoreleasepool {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
NSString *type = [result stringForColumn:@"legislative_type"];
[item setObject:type forKey:@"legislative_type"];
NSString *party = [result stringForColumn:@"legislative_name"];
[item setObject:party forKey:@"legislative_name"];
[list addObject:item];
[item release];
}
}
-(NSString*)stringForColumn:(NSString*)columnName{return[self-stringForColumnIndex:[self-columnIndexForName:columnName]];}它们不一样吗?您是否同时修复了
类型和参与方?您可以使用AliSoftware提供的完整代码。我不明白你所说的“相同”是什么意思。谢谢阿里的回复,我已经这样做了,并且得到了相同的错误,这是仪器泄漏的屏幕截图,我看到了分配,但你的仪器捕获中没有泄漏对不起,阿里,如果我不清楚,听到的是调用树和方法定义的屏幕截图。调用树-可能Instruments捕获内存太早了尝试将while
循环的内部内容嵌入到“@autoreleasepool{…}”语句中,以尽快清除autoreleased对象,而不是等待FoodReporter结束runloop。你试过静态分析器工具吗?(菜单“产品”>“分析”)重新计数无效;别这么说。