我在我的项目中使用 FMDB,并且我已经使用 Instrument for Memory leaks 分析了我的项目。我在 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];
}
任何帮助,将不胜感激。提前致谢