Why is Dispose being called?

In my application, I see that at times the Dispose method in my main form is being called for seemingly no reason. I am not closing the application through the UI, I am not sending a message with the windows closed, or I am calling Close () anywhere, however the Dispose method is called anyway. Here is the call stack:

Bitter.Shell.exe!Bitter.Shell.MainForm.Dispose(bool disposing = true) Line 853 C#
  System.dll!System.ComponentModel.Component.Dispose() + 0x12 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.ApplicationContext.Dispose(bool disposing) + 0x35 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows() + 0x33 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.Dispose(bool postQuit) + 0xf8 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x276 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes 
  System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes 
  Bitter.Shell.exe!Bitter.Shell.Program.Main() Line 105 + 0x26 bytes C#

      

Can the CLR call this if the memory was low when trying to clean up? I know Windows Mobile does exactly that, but I didn't think it happened in the desktop world. Does anyone know why this can be called?

EDIT: After reboot, I don't see this issue anymore. So it seems like it was somehow due to the state of my system at the time. In any case, the reason must be identifiable.

+2


source to share


4 answers


Is an exception thrown on the UI thread?



+2


source


Add a breakpoint to your dispose method and follow the call stack to see what is calling dispose in your code. NET does not call dispose at any time unless your application is shut down by the system or your program itself.



An exception should be thrown. Are you nested message loops?

+2


source


Are you sure your form won't close?

EDIT : Click Debug, Exceptions, take a VS break on all managed exceptions and see if there are any exceptions being swallowed.

+2


source


Try / catch Application.Run in Program.cs as mentioned in a comment on Jon Skeet, the answer won't throw all exceptions.

I would recommend that you add a handler for Application.ThreadException before calling Application.Run:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

    try
    {
        ...
        Application.Run(new MainForm());
    }
    catch (Exception ex)
    {
        ... handle exception ...
    }
}

private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
   ... handle exception ...
}

      

+1


source







All Articles