views:

64

answers:

2

Hi everyone, 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?

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

   //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. 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. 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..

thanks

tldr: I just need to know if I need to manually remove handlers for the events in Formanimator class.

+1  A: 

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.

Vlad
+3  A: 

In your Dispose function, you should detach your function references.

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

John Gietzen
+1 for Week references. Saved me typing my answer out now :-).
Preet Sangha
Sorry, this dispose should go in the FormAnimator class or the actual form?
masfenix
if its in the formanimator class, is it neccessary to "dispose" or can i put that in the destructor of Formanimator?
masfenix
Your `toastform ` should dispose `this.m_Animator`, and your `FormAnimator` should have the code I posted.
John Gietzen