Reputation: 359
My Visual Studio 2019 has started throwing an unhandled exception "System.StackOverflowException" in one of my projects - The exception seems to happen at random places. First it was at a call to ActiveDirectory and after I remove that code the exception just came a at another place(getting a appsetting)
There has been no major changes to the projekt so I really dont know where to look for the error
Currently I cant debug the projekt because the error always comes when i start debugging - If VS is not open there is still a error that says the following
Do any one have an ide to what is giving this error? Other project runs fine
Upvotes: 0
Views: 2736
Reputation: 6524
Following is a classic example of StackOverFlow exception:
public long CalculateFactorial(int number)
{
//if (number == 0)
//{
// return 1;
//}
return number * CalculateFactorial(number-1);
}
This method keeps calling itself and causes a StackOverflow exception. You should check your code and see if missing an exit condition.
Or consider below:
private string userName = "user";
public string UserName
{
get {return userName;}
}
Now, the above is OK. However, if you have a typo and the code becomes like below:
public string UserName
{
get {return UserName;}
}
This will cause a StackOverflow exception.
More on the exception here:
https://learn.microsoft.com/en-us/dotnet/api/system.stackoverflowexception?view=netframework-4.8
Upvotes: 1
Reputation: 4774
A stack overflow will occur when you have a recursion that won't end. It may also happen when you have a recursion that does end, but accumulates many (recursive) calls before it does, since the default stack frame for a .NET program has a limited size.
Either way, you could turn on Visual Studio's built-in profiler and run your program. After it crashes, study the profiler's output to find out which method is called very many times in order to identify where the (infinite) recursion occurs.
Upvotes: 2