MonoTouch/MT 垃圾收集器:如何正确释放我的视图/类成员(简单示例)? [英] MonoTouch/MT Garbage Collector: how to correctly release my views/class members (simple example)?

查看:20
本文介绍了MonoTouch/MT 垃圾收集器:如何正确释放我的视图/类成员(简单示例)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的示例,它没有完全实现,但演示了我在说什么:

Have a look at the sample below which is not completely 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();    
  }
}

  • 如果我点击按钮添加子视图,成员变量 SubViewALabelInTheSubView 被分配了 UIView 的新实例UILabel.
  • 如果我然后单击按钮删除子视图,SubView 将从超级视图中删除.
  • this.SubView.RemoveFromSuperView() 之后,成员SubViewALabelInTheSubView 仍然有对视图或标签的引用,因此不会释放任何内存.到目前为止正确吗?
  • 如果我现在再次单击按钮添加子视图,成员将被 UIViewUILabel 的新实例覆盖.
    • If I click the button to add the subview, the member variables SubView and ALabelInTheSubView are assigned new instances of UIView and UILabel.
    • If I then click the button to remove the subview, SubView gets removed from super view.
    • After this.SubView.RemoveFromSuperView() the members SubView and ALabelInTheSubView still have references to the view or label, hence no memory will be released yet. Correct so far?
    • If I now click the button to add the sub view again, the members will be overwritten with NEW instances of UIView and UILabel.
    • 问题:GC 现在是否知道它现在可以安全地处理以前分配的 UIViewUILabel?我的意思是,所有引用都应该消失.或者我是否必须在从超级视图中删除后调用 this.SubView.Dispose() 和 this.ALabelInTheSubView.Dispose() ?并且根本不需要处理标签,因为它是 UIView 的子节点,它刚刚被删除和处理(这意味着我总是必须从下到上处理)?

      QUESTION: Does the GC now know that it can now safely dispose the previously assigned UIView and UILabel? I mean, all references should be gone. Or do I have to call this.SubView.Dispose() and this.ALabelInTheSubView.Dispose() after removing from superview? And is disposing the label necessary at all, since it is a child node of the UIView, which just got removed and disposed (which would mean I always have to dispose from bottom to top)?

      添加.附带问题:如果我在一个仍然被引用的对象上调用 Dispose() - 这是一个问题吗?

      ADD. SIDE-QUESTION: If I call Dispose() on an object this IS still referenced - is that a a problem?

      推荐答案

      答案是肯定的,垃圾收集器现在会认为对象的前两个实例无法从任何地方访问,并会在下一次收集GC 运行.

      The answer is yes, the garbage collector will now consider the two previous instances of the objects to not be reachable from anywhere, and will be collected the next time the GC runs.

      您不需要手动调用 Dispose().手动调用 Dispose() 的唯一原因是,如果您知道您指向的对象是某个大对象,并且您想确保立即释放内存,而不是等待 GC 启动未来某个时间点.

      You do not need to call Dispose() manually. The only reason to call Dispose() manually is in cases where you know that the object you are pointing to is some large object and you want to make sure that the memory is released immediately, and not wait for the GC to kick-in at some point in the future.

      在对象上调用 Dispose() 会发生什么,是我们在内部释放了对 Objective-C 对象的引用,并将对象上的 Handle 属性设置为 null.这意味着,如果您尝试在已处置的对象上调用另一个方法,您将得到一个很好的异常.

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

      这篇关于MonoTouch/MT 垃圾收集器:如何正确释放我的视图/类成员(简单示例)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆