MonoTouch / MT Garbage Collector: how to properly release my views / class members (simple example)?

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() { } // Imagine our view has a button. When clicked, a new sub view is added. public void HandleButtonAddView() { // Assign a new view to the SubView member and add a label. this.SubView = new UIView(); this.ALabelInTheSubView = new UILabel("A label"); this.SubView.AddSubView(this.ALabelInTheSubView); // Add the SubView to myself. this.AddSubView( this.SubView ); } // Imagine our view has another button. When clicked, the sub view is removed. public void HandleButtonRemoveView() { this.SubView.RemoveFromSuperView(); } } 
  • 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?

+4
source share
2 answers

Answer: yes, the garbage collector will now consider that the two previous instances of objects cannot be accessed anywhere, and will be collected the next time the GC starts.

You do not need to call Dispose () manually. The only reason to manually call Dispose () is when you know that the object you are pointing to is some kind of large object, and you want to make sure that the memory is released immediately, and not wait for the GC to start working in future.

What happens when you call Dispose () on an object is that we internally release the reference to the Objective-C object, and we set the Handle property of the object to null. This means that if you try to call another method on a remote object, you will get a good exception.

+3
source

I would just use your SubView instead of creating a new one every time. So create it in ViewDidLoad, save it in a member variable and reuse it.

I don’t think that additional memory will be a problem and you don’t have to worry about GC at all.

+1
source

Source: https://habr.com/ru/post/1344091/


All Articles