我有一個NSMutableArray這樣定義的:xCode - 爲什麼這個內存泄漏?
@property (nonatomic, retain) NSMutableArray *cList;
我已妥善安置在我的dealloc釋放到CLIST,並選擇我從數據庫中retrive一些數據:
sqlite3 *database;
if(sqlite3_open([self.filePath UTF8String], &database) == SQLITE_OK) {
NSString *sqlStatement = [NSString stringWithFormat:@".....", self.someData];
sqlite3_stmt *compiledStatement;
if (self.cList != nil) {
[self.cList release];
self.cList = nil;
}
self.cList = [[NSMutableArray alloc] init];
if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [self.someString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 2, [self.someOtherString UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
MyModel *newM = [[MyModel alloc] init];
newM.d = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 0)];
newM.c = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
newM.i = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
[self.cList addObject:newM];
[newM release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
當我使用儀器運行它顯示我在這一行的一些泄漏:
self.cList = [[NSMutableArray alloc] init];
...
MyModel *newM = [[MyModel alloc] init];
newM.d = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 0)];
newM.c = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
newM.i = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
[self.cList addObject:newM];
與泄漏的對象:NSCFString和MyModel。爲什麼?我已經正確釋放了cList對象。
像你說的取代消除了所有的泄漏。謝謝。但你能解釋我爲什麼泄漏嗎? – CristiC 2011-04-18 15:28:43
第二塊中的警告源於相同的根本原因。因爲newM被添加到cList,所以當cList泄漏時它也會泄漏。 – 2011-04-18 15:30:12