locked
Windows.Forms Close and Dispose RRS feed

  • Question

  • According to specs, Form objects does not get disposed after calling Close, if they were shown modal (ShowDialog). In a .NET Compact Framework 2.0 application (Windows Mobile 6) we encounter some forms (or individual controls) being disposed without explicitly calling their or their parents' Dispose method. These controls are on forms we create once and shown with ShowDialog when the user selects an item and then the user closes them (Close() method). The unexplained Dispose-s occur only on field, cannot be reproduced.

    Is NET Compact Framework's modal Forms behaves differently regarding Close and Dispose?

    Saturday, May 5, 2018 6:30 AM

All replies

  • Maybe Dispose is called by “finaliser”, which happens when Garbage Collector detects that the group of objects is not referenced.

    Saturday, May 5, 2018 7:22 AM
  • The form has references (member in a collection) so the finalizer cannot dispose it.
    Saturday, May 5, 2018 7:33 AM
  • Hi M.l.T. Systems,

    Form.Close() sends the proper Windows messages to shut down the win32 window. During that process, if the form was not shown modally, Dispose is called on the form. Disposing the form frees up the unmanaged resources that the form is holding onto.

    If you do a form1.Show() or  Application.Run(new Form1()), Dispose will be called when Close() is called.

    However, if you do form1.ShowDialog() to show the form modally, the form will not be disposed, and you'll need to call form1.Dispose() yourself.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Wednesday, May 9, 2018 9:27 AM
    Wednesday, May 9, 2018 8:12 AM
  • Be aware, Wendy describes very well the problem in a practical way (forum format) and makes quick the difference clear. However, her solution is the right one. 

    Success
    Cor




    Wednesday, May 9, 2018 9:30 AM
  • As I mentioned in the original question the form is modal, opened by ShowDialog. What you describe is the original specs I was referred to. 

    Apparently something is different in Compact Framework.

    Wednesday, May 9, 2018 9:45 AM
  • Yea but the lifecycle of your latest  version of  compact framework 2.0 ended in 2003 maybe the year Wendy went the first time to school and therefore there is few chance to get a real answer in these forums. 

    https://suhttps://support.microsoft.com/en-us/lifecycle/search/1143pport.microsoft.com/en-us/lifecycle/search/548


    Success
    Cor


    Wednesday, May 9, 2018 10:03 AM