我在我的项目中使用FMDB,并且我使用Instrument for Memory泄漏来描述我的项目。我在FMResultSet类的这个函数中发现了很多泄漏。泄漏是在这一行:
return [NSString stringWithUTF8String:c];
任何人都可以解释为什么会发生这种情况并为其解决方案吗?因为它导致了大量泄漏事件。
这是我的代码:
//this is how i query and get result
FMResultSet *queryResult = [db executeQuery:queryString withArgumentsInArray:args];
//populate array from result set
NSMutableArray *resultArray = [self populateFromDB:queryResult];
//code of populateFromDB method
- (NSMutableArray*)populateFromDB:(FMResultSet*)queryResult
{
NSMutableArray *resultArray = [[[NSMutableArray alloc]init] autorelease];
while ([queryResult next])
{
//custom model class
MModel *model = [[MModel alloc] init];
model.property1 = [queryResult stringForColumn:@"column1"];
model.property2 = [queryResult stringForColumn:@"column2"];
[resultArray addObject:model];
[model release];
}
return resultArray;
}
//init method of model class
- (id)init
{
if (self == [super init])
{
//nothing..
}
return self;
}
//dealloc method of model class
- (void)dealloc
{
[super dealloc];
}
//code of stringForColumn method of FMResultSet
- (NSString*)stringForColumn:(NSString*)columnName
{
return [self stringForColumnIndex:[self columnIndexForName:columnName]];
}
//code of stringForColumnIndex method of FMResultSet
- (NSString*)stringForColumnIndex:(int)columnIdx
{
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
return nil;
}
const char *c = (const char *)sqlite3_column_text([_statement statement], columnIdx);
if (!c) {
// null row.
return nil;
}
return [NSString stringWithUTF8String:c];
}
任何帮助将不胜感激。提前致谢
答案 0 :(得分:0)
根据SQLLite docs:
用于保存字符串和BLOB的内存空间会自动释放。
可能是C字符串c
从Instruments的角度泄漏,因为SQLLite不透明地管理该字符串的内存。您可以通过检查泄漏对象的类型来确认。 (它是malloc?还是某种CFString对象?)