Memory leaks UIPageViewController内存泄漏

Memory leaks UIPageViewController内存泄漏,memory-leaks,uipageviewcontroller,Memory Leaks,Uipageviewcontroller,UIPageViewController似乎永远持有初始内容视图控制器。 例如: DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard]; NSArray *viewControllers = @[startingViewController]; [self.pageViewController setViewCon

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;
启动视图控制器只有在页面视图控制器本身释放后才会释放

要重现这个bug,只需使用基于页面的应用程序模板在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]从未解除分配

有什么想法吗?
谢谢

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

[startingViewController发布]其中是初始化的结束点


然后将释放第一个ViewController。

是否使用transitionStyle UIPageViewControllerTransitionStyleScroll?我遇到了相同或类似的问题,当使用页面卷曲动画时,这些问题似乎消失了

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


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

我不确定您是否仍然存在问题,但我也遇到了同样的问题,我找到了解决办法

我不知道原因,但它起作用了

我将在
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将在正确的时间解除锁定

还有,我找到了if call

        [self.pageViewController setViewControllers:@[[self viewcontrollerAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished){
        NSLog(@"finished : %d",finished);
    }];
addSubView
之前,完成块将不会调用

我想这就是为什么第一个viewController没有解除锁定的原因

我会找出它为什么没有回调,并改进答案~


干杯

经过几次尝试,我发现在我的项目中,有两个原因导致了保留问题,导致UIPageViewController被永久保留

1) 显示的UIPageViewController和UIViewcontroller之间存在一个循环引用(通过在两个类中将属性从强更改为弱来修复此问题)

2) 主要的解决办法是改变

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

我希望这能帮助别人解决同样的问题 我通过将初始viewController保留在变量中来解决此问题
而不是在特定的页面索引上创建相同的vc,我只是重复使用它

你有没有用ARC找到解决方案?是的,我使用的是滚动样式。我实现了我自己的PageView,因为没有一个开源实现满足我的要求。你能帮我处理代码片段吗?提前谢谢。是的,这是唯一对我也有效的解决方案!文档中没有将此作为一项要求,但这是我调用dealloc的唯一方法。
__weak __typeof__(self) weakSelf = self;
    [weakSelf setViewControllers:@[initialDetailsViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];