简体   繁体   English

GC会收集带有函数指针的对象吗?

[英]Will GC collect an object with function pointers?

just a quick question. 只是一个简单的问题。 First of all, let me verify that I have the correct meaning of a function pointer. 首先,让我验证一下函数指针的正确含义。 In the case of C#, a function pointer is basically just an event function am i right? 在C#的情况下,函数指针基本上只是一个事件函数,我是对的吗?

second, consider the following snippet: 第二,考虑以下片段:

public FormAnimator(Form form) 
{ 
    this.m_Form = form;       
    this.m_Form.Load += new EventHandler(m_Form_Load);
    this.m_Form.VisibleChanged += new EventHandler(m_Form_VisibleChanged);
    this.m_Form.Closing += new CancelEventHandler(m_Form_Closing);  
}

where m_Form is a private variable of type 其中m_Form是类型的私有变量

   //The form to be animated. 
    private Form m_Form;  

Heres how the class is instantiated: 下面是如何实例化类:

public partial class toastform : Form
{   
    public toastform(skImage ic) : this() {

        //Attach this form to the Formanimator. 
        //The FormAnimator now has a reference to this toastform.
        //When the load() of this form is invoked, the Form animator intercepts it and displays the form.
        this.m_Animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up, 400);

        }

so when i create a new toastform (with the something = new toastform();) and call the Show() the show method SHOULD be the one from the form animator. 因此,当我创建一个新的toastform(使用something = new toastform();)并调用Show()时,show方法应该是来自形式animator的方法。 Now, when the toastform closes, how do I make sure the FormAnimator object is also destoryed.. if someone can please explain the full story of what is happening, i'd appreciate that. 现在,当toastform关闭时,我如何确保FormAnimator对象也被破坏...如果有人能够解释正在发生的事情的完整故事,我会很感激。 what i mean is that .. do the toastform class, and the formanimator class both point to the same object, is my lingo right when i say form animator "intercepts" the taostform's events and so on.. 我的意思是..做toastform类,formanimator类都指向同一个对象,当我说形式动画师“拦截”taostform的事件等时,我的术语是正确的。

thanks 谢谢

tldr: I just need to know if I need to manually remove handlers for the events in Formanimator class. tldr:我只需要知道我是否需要手动删除Formanimator类中事件的处理程序。

In your Dispose function, you should detach your function references. Dispose函数中,您应该分离函数引用。

protected override Dispose(bool disposing)
{
    ....

     this.m_Form.Load -= new EventHandler(m_Form_Load);
     this.m_Form.VisibleChanged -= new EventHandler(m_Form_VisibleChanged);
     this.m_Form.Closing -= new CancelEventHandler(m_Form_Closing);  
}

Or, you can use Weak References . 或者,您可以使用弱引用

Here is a very, very good article on weak references: 这是一篇关于弱引用的非常非常好的文章:

http://diditwith.net/PermaLink,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx http://diditwith.net/PermaLink,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx

You need to. 你需要。 The garbage collection runs at some unpredictable time. 垃圾收集在一些不可预测的时间运行。 The object which is not referenced will be garbage collected, but you never know when. 未引用的对象将被垃圾收集,但您永远不知道何时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM