Object destroying problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman S. Golubin1709176985

    #1

    Object destroying problem...

    Hello everybody!

    I wrote an project with one class (Class) and two forms (Form1, Form2):

    -----------------------------------Form2AndClass1B egin----------------------
    ------------------
    public class Class
    {
    public void Form2_Closed(ob ject sender, System.EventArg s e)
    {
    ((Form2)sender) .Closed -= new System.EventHan dler(Form2_Clos ed);
    }
    ~Class()
    {
    System.Windows. Forms.MessageBo x.Show("~Class destroyed");
    }
    }

    public class Form2 : System.Windows. Forms.Form
    {
    .....
    }

    -----------------------------------Form2AndClass1E nd------------------------
    ----------------

    and application form Form1:

    -----------------------------------Form1Begin-------------------------------
    ---------
    public class Form1 : System.Windows. Forms.Form
    {
    private System.Windows. Forms.Button button1;
    ....
    private void button1_Click(o bject sender, System.EventArg s e)
    {
    Form2 frm = new Form2();
    Class cls = new Class();
    frm.Closed += new System.EventHan dler(cls.Form2_ Closed);
    frm.Show();
    frm = null;
    cls = null;
    }
    [STAThread]
    static void Main()
    {
    System.Windows. Forms.Applicati on.Run(new Form1());
    }
    }
    }
    -----------------------------------Form1End---------------------------------
    -------

    After Application starts and I Click on button1, then close Form2, then
    objects Form2 and Class1 must be disposed and destroyed automatically
    (project don't have any reference on its at this time!!!)??!!

    Say me, please, why objects Form2 and Class1 destroyed periodically - from
    time to time??

    --
    thank in avanced,
    WBR, Roman S. Golubin

    PS: I sorry for my bad english. Really sorry ;-(








  • 100

    #2
    Re: Object destroying problem...


    Hi, Roman[color=blue]
    > After Application starts and I Click on button1, then close Form2, then
    > objects Form2 and Class1 must be disposed and destroyed automatically
    > (project don't have any reference on its at this time!!!)??!!
    >
    > Say me, please, why objects Form2 and Class1 destroyed periodically - from
    > time to time??[/color]

    It takes time until GC collects the non-referenced objects. In your case it
    will take even 2 passes until the memory gets reclaimed.
    In the first pass Class1 finalizer will be called and at this point Form1
    and Class1 will become ready for garbage collecting. The actual collection
    will take place during the second pass.
    If you try this in a simple test project GC may not occur unless you close
    the application.

    HTH
    B\rgds
    100


    Comment

    Working...