自定义容器视图控制器 - 添加子视图时看到内存泄漏的工具

时间:2013-10-12 10:15:52

标签: ios objective-c memory-leaks

我制作了一个自定义容器控制器,它包含两个控制器之间的切换。当我将第一个控制器的视图添加到我的容器控制器时,仪器显示内存泄漏。我目前有以下实现:

·H

#import <UIKit/UIKit.h>

@interface SCLDefaultSwitchViewController : UIViewController
{
@protected
    __weak UIViewController *_activeController;
}

@property (nonatomic, weak, readonly) UIViewController *activeController;
@property (nonatomic, weak) UIViewController *leftViewController;
@property (nonatomic, weak) UIViewController *rightViewController;

@end

的.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // set-up content container view
    self.view.autoresizesSubviews = YES;

    [self performSegueWithIdentifier:@"left" sender:self];
    NSAssert( (_leftViewController != nil) , @"left view controller shouldn't be nil!");
    _activeController = _leftViewController;

    [self addChildViewController:_activeController];
    [self.view addSubview:_activeController.view];   // ---> memory leak here!!!!!
    [_activeController didMoveToParentViewController:self];

    [self performSegueWithIdentifier:@"right" sender:self];
    NSAssert( (_rightViewController != nil) , @"right view controller shouldn't be nil!");
    [self addChildViewController:_rightViewController];
    [_rightViewController didMoveToParentViewController:self];

    //[self performSelectorInBackground:@selector(performBackgroundTasks) withObject:nil];
}

我确保在两个视图之间切换时,视图已从其超级视图中正确删除:

- (void) didTapOnStartButton:(SCLSplashScreenViewController *)sender
{
    //CGRect originFrame   = _contentContainerView.bounds;
    CGRect originFrame   = self.view.bounds;
    CGRect offsetFrame   = originFrame;
    offsetFrame.origin.x =  originFrame.size.width;

    UIViewController* toViewController   = self.rightViewController;
    UIViewController* fromViewController = self.leftViewController;

    toViewController.view.frame = offsetFrame;

    __weak SCLSwitchViewController *weakSelf = self;
    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:.4f
                               options: UIViewAnimationOptionCurveEaseOut
                            animations:^{
                                weakSelf.rightViewController.view.frame = originFrame;
                            }

                            // remove the splash screen controller when the animation is finished and delete it: we don't need it anymore
                            completion:^(BOOL finished){
                                if (finished){
                                    [weakSelf.leftViewController willMoveToParentViewController:nil];
                                    [weakSelf.leftViewController.view removeFromSuperview];
                                    [weakSelf.leftViewController removeFromParentViewController];
                                    weakSelf.leftViewController = nil;
                                    weakSelf.activeController = toViewController;
                                }
                            }];
}

我真的没有看到它应该泄漏的任何理由:任何想法?

2 个答案:

答案 0 :(得分:0)

为什么要再次添加新的viewControllers?如果在Storyboard中使用正确的标识符设置segue,则不需要手动添加childViewControllers。把它留给segue。尝试将代码更改为:

- (void)viewDidLoad
{
[super viewDidLoad];

// set-up content container view
self.view.autoresizesSubviews = YES;

[self performSegueWithIdentifier:@"left" sender:self];

[self performSegueWithIdentifier:@"right" sender:self];

//[self performSelectorInBackground:@selector(performBackgroundTasks) withObject:nil];
}

然后在'prepareForSegue:'中将您的Active VC设置为LeftVC,而不是使用全局对象,而是使用segue本身的'destinationViewController'。也就是说,为什么你这样做有点令人困惑,所以关于你想要实现的目标的更多信息会有所帮助。

麦克

答案 1 :(得分:0)

要确定您的代码发生了什么,我们需要了解View Propery中究竟是什么。但不是来自你的弱引用,而是来自你的_leftViewController。由于它是活动控制器及其第一个控制器,因此您将其泄漏添加到实际类的内部,而不仅仅是添加该视图。

[self performSegueWithIdentifier:@"left" sender:self];
NSAssert( (_leftViewController != nil) , @"left view controller shouldn't be nil!");
_activeController = _leftViewController; /**Check Inside _leftViewController. **/

[self addChildViewController:_activeController];
[self.view addSubview:_activeController.view];   // ---> memory leak here!!!!! 

我们需要看到你的代码INSIDE _leftViewController来确定发生了什么。您添加的哪些子视图未在dealloc时删除。即使启用了ARC,有时也在循环中,某些对象会延迟更长时间并产生一些内存泄漏。所以使用

@autorealease {}

autorelease片段可以帮助解决这些问题。

但就像我说的那样,我们需要使用该代码来确定您的代码究竟发生了什么。但我的赌注是问题在那里。(有你提供给我们的证据)。