2012-04-30 31 views
0

我有我的sqlite执行查询代码如下。 Instruments在NSDictionary alloc和release行中捕获内存泄漏(在while循环中)。有人能指出这些分配/释放有什么问题吗?sqlite3 NSDictionary内存泄漏

- (NSMutableArray *)executeQuery:(NSString *)sql arguments:(NSArray *)args { 
    sqlite3_stmt *sqlStmt; 

    if (![self prepareSql:sql inStatament:(&sqlStmt)]) 
     return nil; 

    int i = 0; 
    int queryParamCount = sqlite3_bind_parameter_count(sqlStmt); 
    while (i++ < queryParamCount) 
     [self bindObject:[args objectAtIndex:(i - 1)] toColumn:i inStatament:sqlStmt]; 

    NSMutableArray *arrayList = [[NSMutableArray alloc]init]; // instrument marks leak 0.1 % on this line 
    NSUInteger rcnt= arrayList.retainCount; 
    int columnCount = sqlite3_column_count(sqlStmt); 
    while ([self hasData:sqlStmt]) { 
     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; // instrument marks leak 13% on this line 
     for (i = 0; i < columnCount; ++i) { 
      id columnName = [self columnName:sqlStmt columnIndex:i]; 
      id columnData = [self columnData:sqlStmt columnIndex:i]; 
      [dictionary setObject:columnData forKey:columnName]; 
     } 
     [arrayList addObject:dictionary]; 
     [dictionary release];// instrument marks leak 86.9 % on this line 
    } 

    sqlite3_finalize(sqlStmt); 
    rcnt=arrayList.retainCount; 
    return arrayList ; 
} 

任何帮助/指针非常赞赏。几天来一直在苦苦挣扎。

回答

0

如果你不使用ARC那么我建议你改变你回线:

return [arrayList autorelease]; 

否则你可能泄漏完整的ArrayList。

+0

感谢您的快速回复。当我尝试在arraylist上使用autorelease时,程序崩溃时“发送到释放实例的消息”。这是否意味着我在其他地方释放arrayList? – zolio

+0

也许意味着你没有在你的executeQuery之外保留/释放属性:参数: 检查你是否保留了函数返回的数组,并且在你完成时释放它。 自动释放返回值是函数中的正确动作。因此,错误很可能在所示功能之外。 – fsaint