我有一个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