Dealloc not being called on ARC app

前端 未结 7 1228
盖世英雄少女心
盖世英雄少女心 2020-12-12 18:21

I have a UIViewController that is pushed onto a container controller and then popped off, and using the allocations instrument, I can see that the view controller is destroy

7条回答
  •  孤城傲影
    2020-12-12 18:59

    I just ran across a similar issue. Do you have any blocks where you are referring to 'self'? I had some blocks for notification observation in my init where I was referring to 'self'. Under ARC self is retained in blocks. My dealloc wasn't getting called and that was where I was removing the observation.

    The trick is to create a __weak (iOS 5+) or __unsafe_unretained (iOS 4.x) reference to your 'self' and use that to access self or any _iVars (these will cause 'self' to be retained as well) in the block. Here's an example.

    __unsafe_unretained TableViewController *weakSelf = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextObjectsDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
            if (weakSelf.tableView) {
                [weakSelf.tableView reloadData];
            }
        }];
    

提交回复
热议问题