iPhone 由于 stringWithUTF8String 导致内存泄漏

标签 iphone memory memory-leaks

我有以下代码,显示了 stringWithUTF8String 语句附近最喜欢的对象的内存泄漏。

我已经声明了该特性的最爱

-(NSMutableArray *) readFavoritesFromDatabase 
{
 // Check if database is present
 [self setDatabaseNameAndPath];
 [self checkAndCreateDatabase];

 // Setup the database object
 sqlite3 *database;

 //Initialize favorites array
 if (favorites == nil) 
 {
  [favorites release];
  favorites = [[NSMutableArray alloc] init];
 }
 else 
 {
  favorites = nil;
  [favorites removeAllObjects];
 }


 // Open the database from the users file system
 if(sqlite3_open([self.dataBasePath UTF8String], &database) == SQLITE_OK) 
 {
  // Setup the SQL Statement and compile it for faster access
  const char *sqlStatement = "select * from Favorites";
  sqlite3_stmt *compiledStatement;

  if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
  {

   // Loop through the results and add them to the favorites array
   while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
   {
    // Create Favorite object and add it to the Favorite array
    Favorite *favorite = [[[Favorite alloc] init] autorelease];

    favorite.cameraID = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 0)];
    favorite.cameraName = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 1)];
    favorite.cameraLink = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 2)];

    [self.favorites addObject:favorite];
    //[favorite.cameraID release];
//    [favorite.cameraName release];
//    [favorite.cameraLink release];
   }

   // If favorite cameras exists in database, then sort the Favorites array 
   if([self.favorites count]>0)
   {

    NSSortDescriptor *favoritesNameSorter = [[NSSortDescriptor alloc] initWithKey:@"cameraName" ascending:YES];
    [self.favorites sortUsingDescriptors:[NSArray arrayWithObject:favoritesNameSorter]];
    [favoritesNameSorter release];
   }
  }

  // Release the compiled statement from memory
  sqlite3_finalize(compiledStatement);
 }

 // Close the database
 if(database !=nil)
 {
  sqlite3_close(database);
  return self.favorites;
 }
 else 
 {
  return nil;
 }
}

请告诉我如何解决这个内存泄漏问题 提前致谢。

最佳答案

使用这种安全的方法:

Favorite *tempFavorite = [[Favorite alloc] init];
self.favorite = tempFavorite;
[tempFavorite release];

通常,在您最喜欢的 dealloc 函数中,您应该在调用 super dealloc 函数之前删除所有对象并清理必要的对象。

使用这种方式,你不需要担心 favorite 是否为 nil,因为 Objective-C 允许调用 nil 对象的方法

问候

梅厄·阿萨亚格

关于iPhone 由于 stringWithUTF8String 导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3649472/

相关文章:

javascript - jQuery addClass 导致窗口在 Android 和 iPhone 上滚动回到顶部

c++ - 在同一个指针上调用 new [] 两次而不调用 delete [] 是否会导致内存泄漏?

ios - 在具有 ARC 启用项目的仪器中的 [[NSNumberFormatter alloc] init] 中出现内存泄漏

c - 通过 fork 减轻内存泄漏

performance - MongoDB 将文档预加载到 RAM 中以获得更好的性能

memory - 执行 scikit-learns 剪影分数时如何修复 MemoryError?

android - volley 请求的匿名监听器导致内存泄漏

iphone - 检测 iPhone 设备背面的 3 次轻击

iphone - FB 返回崩溃应用程序 xcode ios iphone

ios - KeyboardWillShowNotification 调用两次 ios 9