Wierd Runtime Error in C#

Status
Not open for further replies.
I've successfully built an application in C# using VS .NET. Everything runs and works fine when I run the application using VS .NET. For some reason unknown to me I get an exception when I run the application using the exe that VS .NET builds. I'm getting an System.StackOverflowException when I exit the program using the X in the top right of the window. Anyone have any ideas why it might be doing this? Could it possibly be because I have a form hidden and it's still in memory? Why wouldn't I get this error when running the application using VS.NET?

Thanks
 
Unfortunately it's a little to big to post. Let me give you some of the background stuff and you might be able to help. It's a chat application. There are two forms. One form is the logon and the other is the chat window. I'm not sure that I am terminating the windows properly.

The logon loads first. If user authentication passes,(through a SQL 2000 DB) the chat window opens and hides the logon window using the following code:

Code:
                        if(password == enteredPassword)
			{
				ActiveForm.Hide();
				FormMain chatWindow = new FormMain(enteredScreenName); 				
				chatWindow.Closed += new EventHandler(OnSubwindowClose);
				chatWindow.Show();
			}

So now the logon window is hidden and the chat window can be seen and used. When I close the chat window I do this:

Code:
		private void FormMain_Unload(object sender, System.EventArgs e, IAsyncResult ar)
		{
			ActiveForm.Close();
		}

So now the chatwindow is closed but the hidden form is still alive. Then this method runs to kill the logon window.
Code:
                private static void OnSubwindowClose(object sender, System.EventArgs e)
		{
			//Application.Exit(); Doesn't work
			ActiveForm.Close(); //This was the original working config
			//ActiveForm.Dispose(); Doesn't work
		}

This then terminates the application when I run it through the debugger. For some reason I get that exception when I run it using the compiled exe.(Tried both the debug and release version)

[Editted] When I debug after using the exe, it tells me that the error is coming from the OnSubwindow close method. Not sure if this helps at all.

THANKS!!!
 

Handor

Ars Tribunus Militum
2,513
Take ActiveForm.Close() out of the Unload event of the form. It's redundant. See, the Unload event (or Closed event) is called at the time the time when the Close() method has already been called; so essentially you're firing the event in the event handler, which, obviously, will cause an infinite loop of function calls, thereby running your stack out of memory.
 
Status
Not open for further replies.