【问题标题】:dismissModalViewController does not release memorydismissModalViewController 不释放内存
【发布时间】:2013-04-13 07:22:56
【问题描述】:

我有两个控制器。在第一个中,有一个按钮,当单击它时,第二个将通过presentModalViewController 方法显示。但是,当我回到第一个时,dismissModalViewController 被调用并且第二个控制器被解除但内存没有被释放。我使用仪器来观察内存分配和弧。

我以为我可能做错了什么,但我尝试了官方示例代码,iPhoneCoreDataRecipes 和 CoreDataBooks,这也发生了。我想知道为什么没有释放内存?

以下是官方示例配方中 presentModal 和 dismissModal 的代码:

- (void)add:(id)sender {
    RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:@"RecipeAddView" bundle:nil];
addController.delegate = self;

Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];
addController.recipe = newRecipe;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
    [self presentModalViewController:navigationController animated:YES];

    [navigationController release];
    [addController release];
}

- (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController didAddRecipe:(Recipe *)recipe {
    if (recipe) {
         // Show the recipe in a new view controller
        [self showRecipe:recipe animated:NO];
    }

    // Dismiss the modal add recipe view controller
    [self dismissModalViewControllerAnimated:YES];
}

我做了类似的事情,但我使用了 arc,所以我没有释放线。

【问题讨论】:

  • 在哪里释放 secondViewDCoroller 的内存?
  • 使用 [secViewCon_Object 发布]; secViewCon_Object = nil;
  • 他不能使用释放,因为他使用 ARC。请出示您的模态视图控制器。

标签: ios objective-c memory-management automatic-ref-counting


【解决方案1】:

您可以在模态视图控制器中创建保留周期。保留周期是指第一个对象对第二个对象具有强引用,而第二个对象也对第一个对象具有强引用。阅读苹果文档中的保留周期:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-1000810

这里:

http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html(使用生命周期限定符来避免强引用循环)

【讨论】:

  • 问题是为什么官方样品有同样的问题?你能看看示例 iPhoneCoreDataRecipes 或 CoreDataBooks 吗?我和第一个类似。
  • 您将 iPhoneCoreDataRecipes 转换为 ARC?
  • 不,我没有,我只是运行示例并检查内存分配。
  • iPhoneCoreDataRecipes 不使用弧。请显示您的泄漏类型。
  • 如果您看到 strdup 泄漏。你不应该担心它,因为它带有滚动视图对象的 iOS 错误,在这里看到这个问题:stackoverflow.com/questions/9880336/…
猜你喜欢
  • 2017-06-29
  • 2011-06-30
  • 2016-09-14
  • 2015-08-19
  • 2012-05-15
  • 2018-01-26
  • 2011-09-30
  • 2012-03-18
  • 2011-08-10
相关资源
最近更新 更多