views:

378

answers:

3

I get an application error thrown when I close the program using as cancel button which all it does is close the form.

The error says: "Application appName.exe encountered a serious error and must shut down"

How do I start fixing it? It is not a thrown exception; no other info is given. What could it be and how do I fix it?

A: 

Throw a try/catch block around the Application.Run(...) line in your Program.cs, like this:

try
{
    Application.Run(new Form1());
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

The message you're seeing means there's a thrown exception that isn't being caught.

MusiGenesis
no actually, it was a system error.
gnomixa
I fixed it; although it was a bit tricky to identify where it happens
gnomixa
Well, put it down as an answer and I'll vote it up. I'm curious to know what it was.
MusiGenesis
A: 

This is typically caused by a system-level exception in your process space that managed code is unable to catch. Typically it happens when you P/Invoke out to native code and give it a bad pointer/parameter and it causes a native exception that is uncaught by the CLR.

ctacke
thanks, makes sense
gnomixa
A: 

Here is what it was. My application has two forms - login and main form where all the action happens. The login form has two buttons (Login and Cancel). Login button logs user in, closes the login form and opens the main form. Cancel button just closes the login form. To close the form I simply used this.Close().

What was happening though is that I still needed to dispose of the login form explicitly by doing something like:

frmLogin.Dispose();
frmLogin = null;

before exiting the program (in my Program.cs)

So this solved it. I had to make sure that this was being done in both cases: when the user logs in as well as when they choose to not log in.

Crucial fact is that frmLogin is modal, hence Dispose() is not called automatically when closed.

gnomixa

related questions