Dealloc not being called on ARC app

前端 未结 7 1196
盖世英雄少女心
盖世英雄少女心 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:45

    Here's another tip (happened to me):

    tl;dr: Look at your class instance variables as well, not just the class properties. Are you allocating any instance variable in code, and not setting it to nil later?

    I had a class with a bunch of properties (@property (nonatomic, strong) and @property (nonatomic, weak)) in its header file. I commented out those properties one by one to see whether this will change anything. It did not. The main class was still not being deallocated. So the problem was not in the properties.

    What I did afterwards was look at the instance variables. I had one instance variable (which was a UIViewController), which I created on viewDidLoad. This one never got dealloc-ed!

    When I removed this variable, sure enough, my main class called dealloc.

    So because this one wasn't dealloc-ed, my main class was not dealloc-ed either. Moving that instance variable as a property solved the problem for me.

    Question: I'm not sure why this happens though. Does the operating system have better control over the properties of a class, than over the instance variables? It seems a bit weird that the parent class which contains the instance variable doesn't get released because of its instance variable.

提交回复
热议问题