sqlite3 NSDictionary内存泄漏

时间:2012-04-30 13:50:12

标签: ios sqlite nsdictionary

我的sqlite执行查询代码如下所示。 Instruments会在NSDictionary的alloc和release行中捕获内存泄漏(在while循环中)。有人能指出那些配置/发布中的错误吗?

- (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 ;
}

非常感谢任何帮助/指针。几天来一直苦苦挣扎。

1 个答案:

答案 0 :(得分:0)

如果您不使用ARC,我建议您更改退货行:

return [arrayList autorelease];

否则你可能正在泄漏完整的arrayList。