Disposing an event/handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?bXIgcGVhbnV0?=

    #1

    Disposing an event/handler

    I have a application which involves creating instances of a "Form2" class.
    Form2 has an event which I handle in Form1. Instances of Form2 have names
    like "Matrix_A", "Matrix_B", etc. To create them I use these "populate"
    methods:

    private void populateA()
    {
    if (!(Matrix_A == null))
    {
    Matrix_A.normal close = false;
    Matrix_A.Close( );
    }
    Matrix_A = new Form2();
    L1: Matrix_A.TheEve nt += new Form2.TheEventH andler(AMethod) ;
    Blah, Blah, other stuff;
    }

    Later, the instance of Form2 may be closed and perhaps recreated several
    times. Do I need to manage (dispose) the event/handler interface created at
    L1 above or does it automatically go away when I close the instance?




  • Jon Skeet [C# MVP]

    #2
    Re: Disposing an event/handler

    mr peanut <mrpeanut@discu ssions.microsof t.comwrote:
    I have a application which involves creating instances of a "Form2" class.
    Form2 has an event which I handle in Form1. Instances of Form2 have names
    like "Matrix_A", "Matrix_B", etc. To create them I use these "populate"
    methods:
    >
    private void populateA()
    {
    if (!(Matrix_A == null))
    {
    Matrix_A.normal close = false;
    Matrix_A.Close( );
    }
    Matrix_A = new Form2();
    L1: Matrix_A.TheEve nt += new Form2.TheEventH andler(AMethod) ;
    Blah, Blah, other stuff;
    }
    >
    Later, the instance of Form2 may be closed and perhaps recreated several
    times. Do I need to manage (dispose) the event/handler interface created at
    L1 above or does it automatically go away when I close the instance?
    This way round, it's okay. Matrix_A will have a reference to the
    instance of Form1, but not the other way round.

    If you were closing the instance of Form1 but keeping Matrix_A around
    (via anothe reference), you would have been wise to unsubscribe to
    avoid Matrix_A keeping the instance of Form1 alive.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    Working...