Take a look at the sample below, which is not fully implemented, but demonstrates what I'm talking about:
public class MyClass : UIView { private UIView SubView; private UILabel ALabelInTheSubView; public override ViewDidLoad() { }
- If I click the button to add a subview, the
SubView and ALabelInTheSubView member ALabelInTheSubView will be assigned new instances of UIView and UILabel . - If I then click the button to remove the view, the
SubView is removed from the super view. - After
this.SubView.RemoveFromSuperView() members of SubView and ALabelInTheSubView still have references to the view or label , so memory will not be released yet. Still? - If I now press the button to add it again under the view, the members will be overwritten by NEW instances of
UIView and UILabel .
QUESTION : Now does the GC know that it can now safely manage previously assigned UIView and UILabel ? I mean, all links should disappear. Or do I need to call this.SubView.Dispose() and this.ALabelInTheSubView.Dispose() after removal from the supervisor? In general, is it necessary to dispose of the label, since it is a child of the node UIView that you just removed and deleted (which would mean that I always need to be arranged from the bottom up)?
ADD. SIDE QUESTION: If I call Dispose () on an object that is still referenced - is this a problem?
source share