NSMutableDictionary initWithContentsOfFile 中的内存泄漏

发布于 2024-10-26 01:28:50 字数 1582 浏览 0 评论 0原文

这是我的代码:(customNames和customNamesArray是静态变量)

-(void) loadCustomDataFromDisk
{
  NSString *fullPath = [self filePathAndFileName: @"customData.plist"];

  if ( ![[NSFileManager defaultManager] fileExistsAtPath: fullPath] )
  {
    NSLog(@"Loading file fails: File not exist");
    customNames = [[NSMutableDictionary alloc] init];
    customNamesArray = [[NSMutableArray alloc] init];
  }
  else 
  { 
    NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
    customNames = [customItems objectForKey: @"customNamesDict"];
    customNamesArray = [customItems objectForKey: @"customNamesArray"];

    if (!customItems)
      NSLog(@"Error loading file");

    [customItems release];
  } 
}

-(void) saveCustomDataToDisk
{
  NSString *path = [self filePathAndFileName: @"customData.plist"];

  NSMutableDictionary *customItems = [[NSMutableDictionary alloc] init];
  [customItems setObject: customNames forKey: @"customNamesDict"];
  [customItems setObject: customNamesArray forKey: @"customNamesArray"];

  BOOL success;
  success = [customItems writeToFile:path atomically:YES];
  if (!success)
    NSLog(@"Error writing file: customDataDict.plist");
  [customItems release];
}

根据Build和Analyze,我在加载customItems时存在潜在泄漏

NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];

,根据Instruments,我确实在该部分存在泄漏。但是当我尝试发布或自动发布自定义项目时,我的应用程序崩溃了。即使我将 NSMutableDictionary 更改为 NSDictionary,我仍然存在泄漏。 我该如何解决这个问题?

任何帮助将非常感激。 :) 谢谢 :)

This is my code: (customNames and customNamesArray are static variables)

-(void) loadCustomDataFromDisk
{
  NSString *fullPath = [self filePathAndFileName: @"customData.plist"];

  if ( ![[NSFileManager defaultManager] fileExistsAtPath: fullPath] )
  {
    NSLog(@"Loading file fails: File not exist");
    customNames = [[NSMutableDictionary alloc] init];
    customNamesArray = [[NSMutableArray alloc] init];
  }
  else 
  { 
    NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
    customNames = [customItems objectForKey: @"customNamesDict"];
    customNamesArray = [customItems objectForKey: @"customNamesArray"];

    if (!customItems)
      NSLog(@"Error loading file");

    [customItems release];
  } 
}

-(void) saveCustomDataToDisk
{
  NSString *path = [self filePathAndFileName: @"customData.plist"];

  NSMutableDictionary *customItems = [[NSMutableDictionary alloc] init];
  [customItems setObject: customNames forKey: @"customNamesDict"];
  [customItems setObject: customNamesArray forKey: @"customNamesArray"];

  BOOL success;
  success = [customItems writeToFile:path atomically:YES];
  if (!success)
    NSLog(@"Error writing file: customDataDict.plist");
  [customItems release];
}

According to Build and Analyze, I have a potential leak in loading customItems

NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];

true enough, according to Instruments, I do have a leak in that part. But when I tried release or autoreleasing customItems, my app crashes. Even if I change NSMutableDictionary to NSDictionary, I still have the leak.
How do I fix this?

Any help would be very much appreciated. :) Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-11-02 01:28:50

您必须保留customNames 和customNamesArray,因为您正在使用字典customItems 中的引用,并且在传递引用后您将释放它。

customNames = [[customItems objectForKey: @"customNamesDict"] 保留];

customNamesArray = [[customItems objectForKey: @"customNamesArray"] 保留];

现在您可以发布自定义项目。

You have to retain customNames and customNamesArray because you are using reference from dictionary customItems and after passing reference you are releasing it.

customNames = [[customItems objectForKey: @"customNamesDict"] retain];

customNamesArray = [[customItems objectForKey: @"customNamesArray"] retain];

Now you can release customItems.

深居我梦 2024-11-02 01:28:50

正如我所见,你的代码是正确的。您可以在这里看到答案,可能会有所帮助 - initWithContentsOfFile 的泄漏问题

我只有一个问题:您创建 NSString *fullPath 并且永远不会释放它。它是自动释放的字符串吗?如果是这样 - 你的代码没问题。

Your code is right as I can see. You can see answer here and may be it helps - Leak problem with initWithContentsOfFile

I have only one question: You create NSString *fullPath and never release it. Is it autoreleased string? If so - your code is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文