locked
How/when to call Dispose when using a *Modeless* Dialog RRS feed

  • Question

  • Hi, this is something I have not been sure of for some time, and have just come across it again, so would like to sort it our once and for all.

    I have a *Modeless* dialog, and when I close it, I notice my GDI climbing, and not appearing to be going down. I do have a couple of 3rd party controls, that implement IDisposable, so when can I actually call Dispose() to attempt to cleanup any such things?

    I did initially call in the FormClosed event, however this appeared to cause the application window to "flash a bit", suggesting to me that I was doing something I should not have been (i.e. Disposing an object while inside one of it's handlers). I did think i doing a a BeginInvoke, to allow this handler to exit and then for Dispose to be called asynchronously, but this seems a little "hacky" to me.

    Is there a "proper" way to do the above (Dispose a modeless dialog), or is calling Dispose() asynchronously what we need to do.

     

    Thanks in advance for any advice

    Peter

     

     

    Friday, August 12, 2011 1:54 AM

Answers

  • Hi Peter,

    What is a modaless dialog in your case? A form you are using as a dialog?

    Anyway, if you are showing a modal form then you have to call Dispose when you don't need it anymore while you don't have to call anything for non modal forms because they take care of calling Dispose when they are closed.

    If your controls are normally placed on form then form will call their Dispose methods for you.

    Now, 3rd party controls might behave badly but there is nothing you can do about it (you can talk to their support). However, you would have to examine the behavior of your application with a memory profiler to know what's going on for sure - there might be static properties, caching, etc. involved and the behavior your are seeing might be very well normal.

     


    Miha Markic [MVP C#] http://blog.rthand.com
    • Proposed as answer by Neddy Ren Tuesday, August 16, 2011 2:31 AM
    • Marked as answer by Neddy Ren Thursday, August 25, 2011 3:26 AM
    Monday, August 15, 2011 8:03 AM

All replies

  • Hi Peter,

    Welcome to the MSDN Forum.

    On how to call the Dispose() method, you can see the specificaton on MSDN:

    IDisposable.Dispose Method:
    http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx 

    On the other hand, your issue should be a third-party issue. You will need to post your questions to the third-party support forum/center.

    If you have any questions, please feel free to tell us.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 15, 2011 6:29 AM
  • Ah, I see, so what you say is this is one situation where you really do need to reply on the Finalizer to clean up any possible unmanaged resources (from a modeless dialog).

    I do know (and have used) this pattern of Dispose before (and using the Finalizer as a "safe guard", but suppressing it if Dispose is expliticlty called)

    In my case, I did end up being able to make the dialog Modal(), hence Dispose could be called by the caller once the dialog is dismissed, howevere I have had other cases of Modeless, where I wanted to clean up.

    People do seem to say top avoid using a Finalizer if possible, but I suppose this could be one place where we do need to reply on one.


    Thanks for your feedback.

     

    regards

    Peter

    Monday, August 15, 2011 6:57 AM
  • Hi Peter,

    What is a modaless dialog in your case? A form you are using as a dialog?

    Anyway, if you are showing a modal form then you have to call Dispose when you don't need it anymore while you don't have to call anything for non modal forms because they take care of calling Dispose when they are closed.

    If your controls are normally placed on form then form will call their Dispose methods for you.

    Now, 3rd party controls might behave badly but there is nothing you can do about it (you can talk to their support). However, you would have to examine the behavior of your application with a memory profiler to know what's going on for sure - there might be static properties, caching, etc. involved and the behavior your are seeing might be very well normal.

     


    Miha Markic [MVP C#] http://blog.rthand.com
    • Proposed as answer by Neddy Ren Tuesday, August 16, 2011 2:31 AM
    • Marked as answer by Neddy Ren Thursday, August 25, 2011 3:26 AM
    Monday, August 15, 2011 8:03 AM
  • You are 100% correct, the Dispose IS called when you close a non modal dialog! I suppose I should have checked for this rather than just assuming it wasn't.

     

    Thanks pointing out that basic oversight on my behalf.

    regards

    Peter

    Tuesday, August 16, 2011 2:38 AM
  • If you let visual studio designer create the form, you'll get a Dispose(bool) function (Microsoft visual studio 2012). You can put a breakpoint in that class

    I used visual studio to create a class Myform : System.Forms.Form with a Closed Event.

    In my main form I added:

    MyForm myForm = new MyForm();

    myForm.Show(this);    // this shows the form as a modeless dialog box

    If I close the form, the folloging functions are called in this order:

    - FormClosing

    - FormClosed

    - Dispose(boo disposingl) // disposing true: meaning IDisposable.Dispose() is used

    So apparently I don't have to call Dispose explicitly, somehow the system takes care that IDispose.Dispose() is called, way before the garbage collector would do this.

    Tuesday, June 4, 2013 9:53 AM