1

I was wondering if someone could help, I am relatively new to programming C#, here is the code that is causing a StackOverflowException:

private void createButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MeWhoService.Account NewAccount = new MeWhoService.Account();

        NewAccount.AccountID = Guid.NewGuid();
        NewAccount.LastName = LastNameTextBox.Text.Trim();
        NewAccount.FirstName = FirstNameTextBox.Text.Trim();
        NewAccount.EmailAddress = EMailAddressTextBox.Text.Trim();
        NewAccount.Password = PasswordTextBox.Password.Trim();
        NewAccount.ConfirmPassword = ConfirmPasswordTextBox.Password.Trim();


        // Set Password
        if (ValidatePassword())
        {
            NewAccount.Password = PasswordTextBox.Password.Trim();
            viewModel.Create(NewAccount);

            NavigationService.Navigate(App.MeAndWhoUri);
        }
        else
        {
            MessageBox.Show("Your Passwords don't match.");
        }
    }
    catch (Exception excp)
    { 
        MessageBox.Show(excp.Message);
    }
}
4
  • It would greatly help if you indicated precisely which lines of the above code are involved in the stack overflow. Commented Dec 21, 2011 at 0:16
  • 1
    A StackOverflowException is caused by a function that ends up calling itself (perhaps indirectly) in a recursive loop until the stack is filled. Often you can see the problem just looking at the stack trace of the exception. You haven't included enough of the code for us to say for sure. Commented Dec 21, 2011 at 0:18
  • 1
    The stack overflow is probably not caused by the above code; instead, it is probably caused by one of the methods that the above code invokes, like ValidatePassword() or something else written by you. Commented Dec 21, 2011 at 0:18
  • You are calling a lot of methods in this method. StackOverflowException occurs when one function calls itself in an indefinite loop. So system dont get enough free memory in stack and hence the exception occurs. Put a breakpoint, and debug and surely you will find a function getting repeated calls... Commented Dec 21, 2011 at 9:22

1 Answer 1

2

Debugging a StackOverflowException in .NET is simple. Start the program in debug, do whatever it is that happens to cause the exception, wait until the exception is thrown and then look at the stack trace. Your stack trace will have a pattern to it showing any number of repeating method calls. Figure out which one of the method calls should not be calling another one in the stack trace and your error will go away.

If you can't find the pattern in your stack trace, include it in your question and I'm sure someone here will be able to help you.

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

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.