0

When I create and show a form and close (terminate) the application when the form is still open, a stack overflow exception is thrown.

Showing the form:

private static void OpenSettings(Object sender, EventArgs e)
{
    ActionLog.Write("Opened Settings");
    form_Settings f_Settings = new form_Settings();
    f_Settings.Show();
}

Closing my Application by using the context menu callback:

private static void Quit(Object sender, EventArgs e)
{
    ActionLog.Write("Exit");
    Settings.Serialize();
    Environment.Exit(0);
}

the exception is thrown in the GUI.form_Settings.Dispose function. The function never exits and causes an infinite recursion.

If I don't have the window opened when I close my application, everything goes fine.

How is that?

// Edit:

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

Guess that's pretty standard, I couldn't find any other definitions. Also I want to mention that I'm using a custom framework (https://github.com/viperneo/winforms-modernui), could this be a reason for this behavior?

9
  • 5
    If the exception is thrown in a Dispose() method, perhaps you should post that method? The code given is not relevant to the problem. Commented Mar 25, 2014 at 20:11
  • What happens if you add a call to f_settings.Close() after the call to Show()? Also, does this happen first time running OpenSettings? Commented Mar 25, 2014 at 20:11
  • Where is Quit called? Commented Mar 25, 2014 at 20:13
  • @TypeIA the function is pretty standard, at least I did not found anything special. I will edit the overriden dispose asap. Sorry. Commented Mar 25, 2014 at 20:16
  • @MikeCheel form is immediately turning black and no, it's even happening after opening the settings form some more times before closing/terminating the application. Commented Mar 25, 2014 at 20:20

2 Answers 2

1

Probably you are victim of a recursive call. Look at the stack trace when the exception is thrown and you will see which methods are called repeatedly. Probably one of the actions in Quit is triggering an event which calls Quit again.

See Recursion on Wikipedia.


UPDATE (in response to your comment)

I would add a flag, telling whether the object was disposed.

private bool _disposed = false;

protected override void Dispose(bool disposing)
{
    if (!_disposed) {
        _disposed = true;

        if (disposing && (components != null))
        {
            components.Dispose();
            components = null; // Now they cannot be disposed again.
        }

        base.Dispose(disposing);
    }
}

But try to understand why it is called recursively. There might be another bug hidden somewhere else.

Sign up to request clarification or add additional context in comments.

4 Comments

Did that already but there was only this specific function over and over again. No other functions were logged, at least the stack trace window doesn't show me more entries.
Well this is exactly the function being called in a recursive way. By "this function" you probably mean Quit. How, where and when is Quit called?
No, the stack trace only contains plenty of entries of this line: Test.exe!Test.form_Settings.Dispose(bool disposing) Line 21 C#. Nothing else. Quit is a context menu callback (if the user clicks on the menu item). The context menu is attached to a NotifyIcon at the start of the application.
Thanks for your snippet but I won't use it since it seems quite dangerous to me. There must be reason why this function actually is recursive and I'm not feeling right about manipulating this since it could effect other modules. Oh and by the way, glad to see a german has answered to my question. :)
0

I just found out that the custom framework ( https://github.com/viperneo/winforms-modernui ) is not working as intended. Everything works fine with the regular Windows Forms, no exception is thrown. But for some reason when using the framework, the exception is thrown if a form is not disposed before the application quits. Hopefully someone may read this when considering using this framework.

Thanks to Hans Passant for the hint regarding the custom framework!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.