【问题标题】:Memory management ARC and view controllers内存管理 ARC 和视图控制器
【发布时间】:2013-11-19 02:31:39
【问题描述】:

我今天在想这个,现在我测试了我有点困惑......

当通过将 viewController 推送到导航堆栈或以模态方式呈现 ViewController 来使用 viewControllers 时,我想知道内存管理。

让我们使用模态示例作为思想实验,这里是创建和呈现视图的源代码,在我的示例中,ARC 与否无关紧要,因此两者兼而有之:

使用 ARC:

ViewController *myViewController = [[ViewController alloc] init];  
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];

没有 ARC:

ViewController *myViewController = [[ViewController alloc] init];  
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];
[myViewController release];                   //As it's now 'owned' by the presenting View controller

这将是我对如何在现有 ViewController 上以模态方式呈现 viewController 的理解。

对于我们的示例,上面的代码驻留在一个方法中,当触摸按钮以呈现 ViewController 时调用该方法。

现在我的问题,

我正在做的是每次触摸按钮时调用此代码,在使用 Instruments 进行测试期间,我似乎没有任何泄漏。 - 然而 因为我在 myViewController deallocviewDidLoad 方法中有 NSLog 语句,所以我知道每次触摸按钮时它都会被实例化,但从未解除分配。

所以……

A) 为什么我没有在仪器中看到泄漏或 Live Bytes 增加)(使用 ARC 或不是)因为我似乎在创建一个新的 viewController 并在每次展示它时泄漏旧的。

B) 如果这不是内存安全的,那么编写上述代码的正确方法是什么?我在 Apple 的示例代码和互联网上都看到了这种代码 sn-ps。我(和他们)是否应该将 alloc init 行包装在 if 语句中以检查对象是否已创建?

if(!myViewController)
{
    ViewController *myViewController = [[ViewController alloc] init];  
}
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];

感谢您抽出宝贵时间阅读和回答,我真的很想知道这一点,因为我一直在使用上面的代码创建、推送和呈现 ViewController,并且从未注意到泄漏! - 可能不得不回去重写它!

为避免混淆,请注意:delegate 属性是我的 UIViewController 子类(我在其中实现了委托协议)的自定义属性,需要正确关闭 Modally 呈现的 Viewcontroller。根据编码指南。

问候, 约翰

按要求编辑,创建委托:

.h

@protocol NotificationManagementViewControllerDelegate;

@interface NotificationManagementController : 
{
    __weak NSObject <NotificationManagementViewControllerDelegate> *delegate;
}
@property (nonatomic, weak) NSObject <NotificationManagementViewControllerDelegate> *delegate;
@protocol NotificationManagementViewControllerDelegate <NSObject>

@optional
- (void)didFinishSettingNotification:(NotificationManagementController *)notificationManagementController;

.m

- (void)sendMessageToDismiss {
    if ([[self delegate] respondsToSelector:@selector(didFinishSettingNotification:)]) {
        [self.delegate didFinishSettingNotification:self];
    }
}

最后是代表.m:

- (void)didFinishSettingNotification:(NotificationManagementController *)notificationManagementController
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller uikit


    【解决方案1】:

    您没有泄漏,因为您创建了一个新控制器,ARC 将为您释放此分配。

    但是,最好为您的新视图控制器创建一个@property。 并修改您的即实现,例如:

    @property (nonatomic, strong) ViewController *myViewController;
    
    if (!_myViewController)
        self.myViewController = [[ViewController alloc] init];  
    
    self.myViewController.delegate = self;
    [self presentViewController:_myViewController animated:YES completion:nil];
    

    在这里,您有一个惰性属性,并且在第一次创建后不会创建新的 ViewController。 但是,您需要在测试之外通过您的委托(或任何属性)。

    此外,如果您使用您的第一个实现并将此控制器添加到当前控制器的子视图中而没有属性,这将起作用,但您会遇到泄漏。 我通过以下代码获得了这种经验:

    RootViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        ViewController *myViewController = [[ViewController alloc] init]; 
        [self.view addSubview:myViewController.view];
    }
    

    myViewController 会被添加到屏幕上,但会立即释放,不会保留对对象的任何引用,因此如果您在 'ViewController` 中添加操作,您的应用程序将在没有 XCode 解释的情况下崩溃。

    所以,写这个不泄漏的正确方法是:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        if (!_myViewController)
            self.myViewController = [[ViewController alloc] init];
    
        [self.view addSubview:self.myViewController.view];
    }
    

    答案有点长,可以改进,所以不要犹豫! 希望它会帮助一些人。

    【讨论】:

    • 好的,但是 ARC 没有释放,因为 myViewControllers dealloc 方法中的 NSLog 从未在控制台中调用/看到。而且 myViewController 已经是一个属性了!
    • @Jordan Montel,作者是菜鸟,他不理解这个问题。问题出在“强委托”上,他只使内存泄漏“一次”,并认为他解决了问题。
    • @l0gg3r 好的,但我试图解释他的代码未来可能发生的泄漏。我不明白反对票? Downvoter 请添加解释!
    • 我不指责任何人,但我不喜欢有人在没有解释的情况下投反对票。我接受批评家的反对意见:)
    • 那么对我上面的额外评论有什么想法吗?我已经拥有了一个属性,而且我也从来没有看到 dealloc 被调用(如果你说它是在 ARC 为我销毁对象时被调用的)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多