locked
Destructor is not called RRS feed

  • Question

  • Hello,

     

    I have a WinForm application I am working on.

    The application has one main Form with a few buttons.  Pressing each of the buttons will open a new Form.  On one of these Forms (the one in question, RunForm) I am instantiating an Object (a class I wrote and named ChannelComm) that has a destructor.

     

    This Form in question (RunForm) is started/called in the following way:

     

      RunForm         dlg = new RunForm();

      DialogResult    res = dlg.ShowDialog();

      dlg.Dispose();

     

    What I am noticing, using a breakpoint, is that the destructor for ChannelComm is not called. Why isn’t it called?

     

     

    Thanks,

    Eitan Barazani

     

     

    Tuesday, January 15, 2008 11:58 PM

Answers

  •  

    Hi cmsalvatore,

    Thank you for posting. Since this forum is global, it would be best if you can post your thread in English. This will make answer easy to read in the forum and be beneficial to other community members as well. Thanks.

    Best regards.
    Rong-Chun Zhang

    Friday, January 18, 2008 3:01 AM
  • I'm so sorry. I also post answers in the spanish forum. I think I just messed up. Here is the transated verision.

     

    May be we should start by saying that the Dispose() method is not the same as the destructor.

    Second, you should have in mind that calling Dispose() doesn't necessarily means that the destructor method will be called inmediatly. Dispose() is intended to let the objects that inherits from Component, free all the resources and get ready for whenever the Garbage Collerctor free the mermory that was assigned to that object. To make sure that this will happen as soon as possible, you should also make sure that the variables that where refering to that object gets a null reference instead.

     

    dlg.Dispose();

    dlg = null;

     

    But once more, that doesn't mean that the GC will collect it inmediatly or that the destructor will be called at that point.

     

    Here are two links that may be usefull for you:

    http://msdn2.microsoft.com/es-es/library/498928w2(VS.80).aspx

    http://msdn2.microsoft.com/es-es/library/fs2xkftw(VS.80).aspx

    Friday, January 18, 2008 9:09 PM

All replies

  •  

    tal vez deberíamos empezar por aclarar que el método Dispose() no es lo mismo que el método destructor.

    en segundo lugar, deberías tener en cuenta que llamar al método Dispose() no significa llamar al método destructor inmediatamente. el método Dispose() está pensado para que los objetos que descienden de la clase Component liberen todos sus recursos y queden listos para que el Garbage Collector (cuando sea que se ejecuta) libere la memoria asignada. para esto hay que tener en cuenta además que es necesario asegurarse de eliminar todas las referencias a ese objeto (o que esperar a que estas salgan del scope de ejecución), en tu ejemplo habría que hacer algo como:

     

    dlg.Dispose();

    dlg = null;

     

    de todos modos esto no significa que automáticamente el objeto será reclamado por el GC.

     

    Te paso dos links que creo te podrán ser de utilidad:

    http://msdn2.microsoft.com/es-es/library/498928w2(VS.80).aspx

    http://msdn2.microsoft.com/es-es/library/fs2xkftw(VS.80).aspx

     

     

     

     

     

     

    Wednesday, January 16, 2008 12:17 AM
  •  

    Hi cmsalvatore,

    Thank you for posting. Since this forum is global, it would be best if you can post your thread in English. This will make answer easy to read in the forum and be beneficial to other community members as well. Thanks.

    Best regards.
    Rong-Chun Zhang

    Friday, January 18, 2008 3:01 AM
  • I'm so sorry. I also post answers in the spanish forum. I think I just messed up. Here is the transated verision.

     

    May be we should start by saying that the Dispose() method is not the same as the destructor.

    Second, you should have in mind that calling Dispose() doesn't necessarily means that the destructor method will be called inmediatly. Dispose() is intended to let the objects that inherits from Component, free all the resources and get ready for whenever the Garbage Collerctor free the mermory that was assigned to that object. To make sure that this will happen as soon as possible, you should also make sure that the variables that where refering to that object gets a null reference instead.

     

    dlg.Dispose();

    dlg = null;

     

    But once more, that doesn't mean that the GC will collect it inmediatly or that the destructor will be called at that point.

     

    Here are two links that may be usefull for you:

    http://msdn2.microsoft.com/es-es/library/498928w2(VS.80).aspx

    http://msdn2.microsoft.com/es-es/library/fs2xkftw(VS.80).aspx

    Friday, January 18, 2008 9:09 PM