UIPageViewController Memory Leak

The UIPageViewController seems to hold the initial content view controller forever. For example:

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; 

startViewController is never released until the pageViewController itself is released.

To reproduce this error, simply create a new project in Xcode using a page-based template. And add 3 lines of code to DataViewController.m.

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

And when you scroll through the demo app in vertical orientation, you will get the following logs:

 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] is never freed.

Any ideas on this? Thank!

+8
memory-leaks uipageviewcontroller
Mar 01 '13 at 11:07
source share
5 answers

Do you use the transition Style UIPageViewControllerTransitionStyleScroll? I ran into the same or similar issue that seemed to disappear when using page curl animation.

The problem was exacerbated for me because I allowed the UISliderBar to set a position in the content. Thus, when changing the UISliderBar, I called setViewControllers: direction: animated: completion: which caused more and more links to the link manager to get stuck in my UIPageViewController.

I also use ARC. I did not find an acceptable way to force the UIPageViewController to release additional view controller links. I will probably either take advantage of going to the curl of the page or implement my own equivalent of UIPageViewController using UIScrollView with paging enabled, so I can manage my own View controller cache instead of relying on the control of the uncontrolled view controller of the UIPageViewController.

+3
Dec 6 '13 at 19:47
source share

I'm not sure if you still have a problem, but I had the same problem and I found a solution.

I do not know the reason, but it works.

I set the first viewController right after addSubview , and not before 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]; } } 

and the first viewController will shut down at the right time.

also, i found if the call

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

until addSubview and the completion block will not be called.

and I believe that this block is the reason why the first viewController did not disable.

I’ll go and find out why he didn’t answer and didn’t improve the answer.

amuses

+3
Jul 23 '15 at 9:35
source share

After several attempts to find out what is happening on a similar problem, I noticed that there were two reasons in my project that caused the save problem and caused the UIPageViewController to stay forever.

1) a circular link was specified between the UIPageViewController and the UIViewcontroller that was submitted (this was fixed by changing the properties from weak to strong in both classes)

2), and the main fix was to change

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

to

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

I hope this helps someone

+1
Aug 26 '16 at 13:19
source share

the same problem here I solved it by storing my original viewController in a variable and instead of creating the same vc on a specific pageIndex, I just reuse it

0
Dec 10 '16 at 13:33
source share

I had the same problem and solved the following:

[startingViewController release]; where is the initialization endpoint.

then the first ViewController will be released.

-one
Jun 07 '13 at 8:28
source share



All Articles