2012-04-30 43 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