When a view controller is dealloc'd under ARC, are it's properties also dealloc'd if another object has a strong reference?

坚强是说给别人听的谎言 提交于 2019-12-11 20:38:17

问题


I have a mapping application that uses a peripheral slide in/slide out right hand view controller, much like the Google Maps application shown below (source):

In my app, this slide out view has a weak delegate property, and I set the map view controller to be the delegate (e.g. mapOptionsViewController.delegate = self from inside myMapViewController). I also wish to use the right hand slide in view for other sorts of information though. E.g. displaying a menu for a restaurant that a user selects on the map, menuViewController.

My question is, if I swap out mapOptionsViewController for menuViewController, do I need to manually nil out the mapOptionsViewController.delegate from myMapViewController also? My concern is that even if mapOptionsViewController is dealloc'd/disposed of when I do the switch to menuViewController, because myMapViewController still points to the old mapOptionsViewController.delegate, mapOptionsViewController.delegate's memory will not be released from the heap.

So basically, if you have an object view controller foo (my map) which is the delegate for another view controller bar (map options), such that bar.delegate = foo, and then you switch out bar for baz (restaurant menu), do I need to set bar.delegate = nil from foo. Or, will bar nil out delegate when the view controller delegate is contained in is dealloc'd as a result of being replaced with baz?


回答1:


So basically, if you have an object view controller foo (my map) which is the delegate for another view controller bar (map options), such that bar.delegate = foo, and then you switch out bar for baz (restaurant menu), do I need to set bar.delegate = nil from foo. Or, will bar nil out delegate when the view controller delegate is contained in is dealloc'd as a result of being replaced with baz?

So, let's see if I understand what you're doing.

You have a bar, with a weak reference to a foo, so bar.delegate = foo. You must also have something else (I'll call it a qux) with a strong reference to the same foo, because otherwise your foo would have disappeared. Let's say the relevant property is qux.menuThingy = foo.

You then do qux.menuThingy = baz. What happens to your bar?

If the qux was the only thing with a strong reference to the foo, there are now no more strong references to the foo, so the foo is deallocated. Because you used a weak reference, bar.delegate has been automatically set to nil for you. You don't have to do it yourself.

If however there was yet another object with a strong reference to the foo, then the foo is still around and hasn't been deallocated.




回答2:


In one word, no. as long as an object still have strong reference, and the last object that have a string reference to it is not deallocated, it is not deallocated. Or, you have a bug.

Also, weak references will be automatically zeroed when the object referencing to it is deallocated. However assign ones will not.



来源:https://stackoverflow.com/questions/16957151/when-a-view-controller-is-deallocd-under-arc-are-its-properties-also-dealloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!