0
爲什麼下面的代碼在iPhone應用程序中導致內存泄漏?使用嵌套數組設置合成數組導致內存泄漏
下面所有初始對象泄漏,包括數組,字符串和數字。所以,我認爲它與合成數組屬性沒有釋放對象有關,當我在第二次和隨後調用這段代碼時再次設置屬性時。
下面是代碼:
「控制器」(下圖)是我的自定義視圖控制器類,我有一個參考,而我用這個代碼片段設置:
sqlite3_stmt *statement;
NSMutableArray *foo_IDs = [[NSMutableArray alloc] init];
NSMutableArray *foo_Names = [[NSMutableArray alloc] init];
NSMutableArray *foo_IDsBySection = [[NSMutableArray alloc] init];
NSMutableArray *foo_NamesBySection = [[NSMutableArray alloc] init];
// Get data:
NSString *sql = @"select distinct p.foo_ID, p.foo_Name from foo as p ";
if (sqlite3_prepare_v2(...) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int p_id;
NSString *foo_Name;
p_id = sqlite3_column_int(statement, 0);
char *str2 = (char *)sqlite3_column_text(statement, 1);
foo_Name = [NSString stringWithCString:str2];
[foo_IDs addObject:[NSNumber numberWithInt:p_id]];
[foo_Names addObject:foo_Name];
}
sqlite3_finalize(statement);
}
// Pass the array itself into another array:
// (normally there is more than one array in each array)
[foo_IDsBySection addObject: foo_IDs];
[foo_NamesBySection addObject: foo_Names];
[foo_IDs release];
[foo_Names release];
// Set some synthesized properties (of type NSArray, nonatomic,
// retain) in controller:
controller.foo_IDsBySection = foo_IDsBySection;
controller.foo_NamesBySection = foo_NamesBySection;
[foo_IDsBySection release];
[foo_NamesBySection release];
謝謝任何幫助!
您是如何檢測泄漏的? – fbrereto 2010-05-19 22:34:11
通過泄漏儀器。我開始懷疑它是否錯誤地看到泄漏。無論如何,我在dealloc過程中遺漏了這些版本。謝謝。 – Webtoad 2010-05-21 12:40:05