【问题标题】:Memory: object not release after view controller dismiss, with ARC内存:视图控制器关闭后对象未释放,使用 ARC
【发布时间】:2013-12-15 08:15:10
【问题描述】:

我有 2 个视图控制器,

FirstViewController -> SecondViewController 通过

[self presentViewController:SVC animated:YES completion:nil];

当我这样做时在 SecondViewContrller 上

[self dismissViewControllerAnimated:YES completion:nil];

我的问题是,为什么我关闭这个视图控制器后对象没有在 secondViewController 上释放。正如您在图表上看到的那样,解雇后它没有下降。顺便说一句,释放/关闭 ViewController 的最佳方式是什么?

[编辑]

当我从 FVC->SVC->[dismiss SVC] 开始时,我在每个 VC 上的 dealloc 方法上 NSLog 一条消息。这是我的日志

【问题讨论】:

  • 您是否正确分配了视图控制器。给你的 viewcontroller 初始化分配代码
  • SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
  • 您可以在您的第二个视图控制器中实现 dealloc 方法并在其中放置一个断点(或 NSLog)。 ARC 并不意味着立即释放。
  • 设置视图控制器的属性
  • @MarcMosby 似乎没有通过 dealloc 方法。我虽然 ARC 会处理这个问题。我应该在dealloc中做什么?

标签: ios objective-c memory-management viewcontroller


【解决方案1】:

这可能是相当粗糙的东西。我以前也有类似的问题。搜索你的代码,看看你是否有对对象的强引用或错误引用。

我的最大错误之一(以及我在互联网上看到的数百次)是委托属性。我像@property (nonatomic, retain) id<protocol>delegate; 一样写了很长时间,因为我意识到如果我这样做,委托对象不会被释放。在这种情况下必须使用assign

希望对你有所帮助...

【讨论】:

  • 您好,感谢您的想法,但就我而言,我没有做任何委托、插座、协议等。这只是一个简单的视图控制器。我的观点是在关闭 VC 时真正检查内存管理。
【解决方案2】:

我已经对此行为进行了一些调查。

FirstViewController.m

 #import "FirstViewController.h"  
 #import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
}
  return self;
}

-(void)dealloc {
    NSLog(@"First Dealloc");

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIButton *pressMe=[UIButton buttonWithType:UIButtonTypeCustom];
    pressMe.frame = CGRectMake(0, 0, 100, 40);
    pressMe.center = self.view.center;
    [pressMe setTitle:@"PressMe" forState:UIControlStateNormal];
    [pressMe addTarget:self action:@selector(pressMeAction:)             
    forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:pressMe];

// Do any additional setup after loading the view.
 }

 - (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void) pressMeAction:(id) sender
{
    SecondViewController *svc = [[SecondViewController alloc] init];
    [self presentViewController:svc animated:YES completion:nil];
    NSLog(@"Present Second");
}

@end

SecondViewController.m

除了

-(void) pressDissmissButtonAction:(id) sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"Dismiss Second");
}

这是分配动态

正如你所见,在 pressMeButtonAction 调用 secondViewController 分配后,在 pressDissmissButtonAction 调用 secondViewController 成功解除分配。

但是:大多数情况下它会立即解除分配,但如果您非常快速地(大约一秒左右)呈现和解除分配,解除分配不会立即触发,而是会在一段时间后触发。

我假设这是 ARC 释放过程的设计实现。不确定。

【讨论】:

  • 您无法在启用 ARC 的情况下实现 -dealloc 方法
  • 这是不正确的。使用 ARC 时,不得在 super 上调用 delloc。 developer.apple.com/library/mac/documentation/Cocoa/Reference/…
  • FirstVC 上的 NSlog on dealloc 方法出来了吗?
  • 不,不是,因为这个控制器是根控制器。
  • 对不起,我以为我无法使用已实现的-dealloc 方法进行编译
【解决方案3】:

试试这个...

[self presentViewController:SVC animated:YES completion:nil];

SVC = nil;

【讨论】:

  • 使用 ARC 时无法发送release 消息。
【解决方案4】:

在花了很多时间之后,我终于找到了一个缺失的部分:你不仅必须将任何对 ViewController 的强引用设置为 nil,你还必须使任何计时器无效并注意块保留周期。 每当您在块中使用 self 时,您都会创建一个保留循环!相反,您应该像这样声明一个变量

__unsafe_unretained ViewController *weakSelf = self; 

并在块中使用它而不是 self。

【讨论】:

  • 感谢您提供此信息。您能否提供内存图表的屏幕截图,一旦您关闭 SecondViewController,它会在哪里消失
【解决方案5】:

检查您的应用程序中的所有 IBOutlets。可能有分配给他们的“强”属性。让他们“软弱”。例如,一个 IBOulet 应该是这样的:

@property (weak, nonatomic) IBOutlet UILabel *myLabel;

检查您的应用程序中的所有委托(如果有)。每个代表都应该是这样的:

@property (nonatomic, assign) id <yourProtocol> delegate;

请注意,ARC 需要一些时间来恢复内存。

【讨论】:

    【解决方案6】:

    计时器是我的问题。为 viewWillDisappear 添加了计时器无效,然后释放了视图控制器。

    【讨论】:

      猜你喜欢
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2018-04-27
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多