我将self.notes数组添加到UIPickerView。这是我设置数组的方式:

 NSMutableArray *notesArray = [[NSMutableArray alloc] init];
 [notesArray addObject:@"-"];
 [notesArray addObjectsFromArray:[dbManager getTableValues:@"Notes"]];
 self.notes = notesArray;
 [notesArray release];


通过以下方法从数据库中获取UIPickerView的信息:

-(NSMutableArray *)getTableValues:(NSString *)table
{
  NSMutableArray *valuesArray = [[NSMutableArray alloc] init];
  if (sqlite3_open([self.databasePath UTF8String], &database) != SQLITE_OK)
  {
      sqlite3_close(database);
      NSAssert(0, @"Failed to open database");
  }
  else
  {
   NSString *query = [[NSString alloc] initWithFormat:@"SELECT value FROM %@", table];
   sqlite3_stmt *statement;

   if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
   {
     while (sqlite3_step(statement) == SQLITE_ROW) {
     NSString *value =[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
     [valuesArray addObject:value];
     [value release];
   }
   sqlite3_reset(statement);
  }
  [query release];
  sqlite3_finalize(statement);
  sqlite3_close(database);
 }
 return valuesArray;
}


但是,在这些行中,我不断在Instruments中发生内存泄漏:

NSMutableArray * valuesArray = [[NSMutableArray alloc] init];



[valuesArray addObject:value];

我在这里做错了什么?

谢谢你的帮助!

最佳答案

代替NSMutableArray *valuesArray = [[NSMutableArray alloc] init];使用此行:

NSMutableArray *valuesArray = [NSMutableArray array];


至于[valuesArray addObject:value];泄漏,将代码更改为此:

[valuesArray addObject:value];
value = nil;

09-16 23:27