【问题标题】:UIPageViewController memory leakUIPageViewController 内存泄漏
【发布时间】:2013-03-01 11:07:26
【问题描述】:

似乎 UIPageViewController 永远持有初始内容视图控制器。 例如:

DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;

startingViewController 在释放 pageViewController 本身之前永远不会释放。

要重现此错误,只需使用基于页面的应用程序模板在 XCode 中创建一个新项目。并在DataViewController.m中添加3行代码

@property NSInteger debugIndex; // file scope
NSLog(@"DataViewController[%d] created", self.debugIndex); // in viewDidLoad
NSLog(@"DataViewController[%d] dealloc", self.debugIndex); // in dealloc

当你在垂直方向滚动演示应用程序时,你会得到这样的日志:

DataViewController[0] created  
DataViewController[1] created  
DataViewController[2] created  
DataViewController[1] dealloc  
DataViewController[3] created  
DataViewController[2] dealloc  
DataViewController[4] created  
DataViewController[3] dealloc  
DataViewController[5] created  
DataViewController[4] dealloc  
DataViewController[6] created  
DataViewController[5] dealloc  

DataViewController[0] 永远不会被释放。

对此有什么想法吗? 谢谢!

【问题讨论】:

  • 您有没有找到 ARC 的解决方案?

标签: memory-leaks uipageviewcontroller


【解决方案1】:

你在使用transitionStyle UIPageViewControllerTransitionStyleScroll吗?我遇到了相同或类似的问题,当使用页面卷曲动画时似乎消失了。

问题对我来说更加复杂,因为我允许 UISliderBar 设置内容中的位置。因此,在更改 UISliderBar 时,我正在调用 setViewControllers:direction:animated:completion: 这导致越来越多的视图控制器引用“卡在”我的 UIPageViewController 中。

我也在使用 ARC。我还没有找到一种可接受的方法来强制 UIPageViewController 放弃额外的视图控制器引用。我可能最终会使用页面卷曲转换,或者使用启用分页的 UIScrollView 实现我自己的 UIPageViewController 等效项,这样我就可以管理自己的视图控制器缓存,而不是依赖 UIPageViewController 损坏的视图控制器管理。

【讨论】:

  • 是的,我使用的是滚动样式。我实现了自己的 PageView,因为没有一个开源实现符合我的要求。
【解决方案2】:

我不确定您是否仍然遇到问题,但我遇到了同样的问题并且我找到了解决方案。

我不知道原因,但它有效。

我在addSubview 之后设置第一个viewController,而不是在addChlidViewController 之前。

-(void)settingPageViewController{
if (!self.pageViewController) {
    self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.pageViewController didMoveToParentViewController:self];
    [self.containerView addSubview:self.pageViewController.view];
    [self.pageViewController setViewControllers:@[[self viewcontrollerAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}    
}

第一个 viewController 会在合适的时间释放。

另外,我发现如果调用

        [self.pageViewController setViewControllers:@[[self viewcontrollerAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished){
        NSLog(@"finished : %d",finished);
    }];

在addSubView 之前,完成块不会调用。

我认为这个块是第一个 viewController 没有释放的原因。

我去看看为什么没有回调,完善答案~

干杯

【讨论】:

  • 是的,这也是唯一对我有用的解决方案!文档没有将此作为要求说明,但这是我让 dealloc 被调用的唯一方法。
【解决方案3】:

在尝试找出类似问题的情况后,我注意到在我的项目中有两个原因导致保留问题并导致 UIPageViewController 被永久保留。

1) 在呈现的 UIPageViewController 和 UIViewcontroller 之间存在循环引用(通过将两个类中的属性从强更改为弱来解决此问题)

2) 主要修复在于改变

[self setViewControllers:@[initialDetailsViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

到

__weak __typeof__(self) weakSelf = self;
    [weakSelf setViewControllers:@[initialDetailsViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

我希望这对某人有所帮助

【讨论】:

    【解决方案4】:

    同样的问题 我通过将我的初始 viewController 保存在变量中来解决它 而不是在特定的pageIndex上创建相同的vc,我只是重用它

    【讨论】:

    • 你能帮我解决一下代码 sn-p 吗?提前致谢。
    【解决方案5】:

    我遇到了同样的问题并解决了以下问题:

    [startingViewController release];这里是初始化的终点。

    那么第一个 ViewController 将被释放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2013-01-20
      • 2011-10-31
      • 2019-08-10
      • 2013-06-24
      • 2011-03-22
      • 2015-04-20
      相关资源
      最近更新 更多