简体   繁体   English

释放UIViewController子类实际上并没有释放内存

[英]releasing UIViewController subclasses does not actually free up memory

I have a UINavigationController object (named LoginNav ) which consists of ViewController1 & ViewController2 , my iPad app starts by loading a UISplitViewController subclass (named mainSplitViewController ) and then presenting LoginNav modally on top of it (this is by the way done in didFinishLaunchingWithOptions method of AppDelegate like this: 我有一个UINavigationController对象(名为LoginNav ),它由ViewController1ViewController2 ,我的iPad应用程序首先加载UISplitViewController子类(名为mainSplitViewController ),然后LoginNav模态形式显示LoginNav (通过didFinishLaunchingWithOptions方法完成)像这样:

[self.mainSplitViewController presentModalViewController:LoginNav animated:YES]; ). )。

Once ViewController1 is shown, I tap a UIButton in it to push ViewController2 , when I finish working in ViewController2 I tap a UIButton in it to call [self.navigationController dismissModalViewControllerAnimated:YES]; 一旦显示了ViewController1 ,我在其中点击一个UIButton来推送ViewController2 ,当我在ViewController2完成工作时,我在其中点击一个UIButton来调用[self.navigationController dismissModalViewControllerAnimated:YES]; to dismiss LoginNav with both of its view controllers and show mainSplitViewController's contents. 用它的两个视图控制器关闭LoginNav并显示mainSplitViewController's内容。

There is a dealloc method in both ViewController1 & ViewController2 with NSLog statement in each one, once loginNav is dismissed, the NSLogs never get fired, but doing [self.navigationController.viewControllers objectAtIndex:0] release]; ViewController1ViewController2 ,每个都有一个带有NSLog语句的dealloc方法,一旦loginNavNSLogs不会被解雇,而是执行[self.navigationController.viewControllers objectAtIndex:0] release]; & [self.navigationController.viewControllers objectAtIndex:1] release]; [self.navigationController.viewControllers objectAtIndex:1] release]; right after [self.navigationController dismissModalViewControllerAnimated:YES]; [self.navigationController dismissModalViewControllerAnimated:YES]; fires both NSLogs . 触发两个NSLogs

I commented out the above two release statements, then I launched the Allocations instrument, and launched the app again and pushed ViewController2 then dismissed loginNav as described above, and looked at Live Bytes column (All Allocations value ) it was 6.9 MB right after the dismissal of loginNav , then I did this step again but in this case using the two release statements, I got exactly a 6.9 MB value on Live Bytes column. 我注释掉了以上两个发布声明,然后启动了Allocations工具,再次启动了应用程序,然后按了ViewController2然后如上所述关闭了loginNav ,然后查看了Live Bytes列(“所有分配”值),它在关闭后为6.9 MBloginNav ,然后再次执行此步骤,但是在这种情况下,使用两个release语句,我在Live Bytes列上获得了6.9 MB值。

Two Questions: 两个问题:

1) why do not the dealloc methods of ViewController1 & ViewController2 never get fired after the dismissal of the navigation controller LoginNav that holds them ? 1)为什么在解雇了拥有它们的导航控制器LoginNav后,从未将ViewController1ViewController2dealloc方法解雇? and is it correct to do the above two release statements to release these view controllers ? 并且执行以上两个release语句来释放这些视图控制器是否正确?

2) why releasing ViewController1 & ViewController2 does not free up memory ? 2)为什么释放ViewController1ViewController2不会释放内存?

ps there is no single variable (or IBOutlet) being held in memory in both ViewController1 & ViewController2 , everything is released in both of them. ps在ViewController1ViewController2内存中都没有单个变量(或IBOutlet) ,所有内容都在它们两个中释放。

These kinds of issues are nearly impossible to troubleshoot without seeing all your code. 这些类型的问题几乎不可能在没有看到所有代码的情况下进行故障排除。 When you manage memory manually, there are multiple areas that you can go wrong. 手动管理内存时,可能会出错。 For example the following code will leak: 例如,以下代码将泄漏:

- (void)didSelectSomethingInViewControllerOne
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
}

In this case you have allocated the object and thus have ownership of it. 在这种情况下,您已经分配了对象,因此拥有它的所有权。 Then the nav controller takes ownership of it. 然后,导航控制器将其拥有。 When you pop the controller from the navigation stack, the navigation controller relinquishes ownership of it, but you never did, so it still has a retain count of 1 and won't get deallocated. 当您从导航堆栈中弹出控制器时,导航控制器会放弃它的所有权,但是您没有这样做,因此它的保留计数仍为1,不会被释放。

Relinquishing ownership of the controllers later in your code (like after dismissing the modal view) is a bad idea. 稍后在代码中放弃控制器的所有权(例如关闭模式视图后)是一个坏主意。 It makes it difficult to analyze ownership when your releases are all over the place. 当发布到处都是时,很难分析所有权。 As soon as the navigation controller has ownership you can release the object you allocated, as you do not intend to use it in the future: 导航控制器拥有所有权后,就可以释放分配的对象,因为您将来不打算使用它:

- (void)didSelectSomethingInViewControllerOne
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
    [vc2 release];
}

The situation above can have nothing to do with your problem. 上面的情况与您的问题无关。 Your problem may reside in many different areas. 您的问题可能存在于许多不同的领域。 Which is why troubleshooting memory management problems is difficult. 这就是为什么很难对内存管理问题进行故障排除。 Without seeing source code. 没有看到源代码。

您可能会犯规的另一件事是LoginNav中的保留属性(例如委托)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM