0

I am a newcomer to world of programming and trying to refactor a Outlook Addin in which all code was written in ThisAddIn.cs i.e. automatically generated file. I have separated contents of ThisAddIn to several class but when I am debugging my application StackOverflow Exception is thrown. What could be the cause for such problem?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    The cause is typically a call loop or un-intended recursion. Without seeing code though, we can't tell you where it is or how to fix it. – Ron Beyer Oct 28 '15 at 14:09
  • Welcome to [so]! Please see the [faq]s and read [ask] a couple of times. We really need you to post the code you think is causing the problem for us to solve it, otherwise check the tips in my answer to troubleshoot it yourself, good luck! – Jeremy Thompson Oct 28 '15 at 14:32

2 Answers2

0

Generally speaking an StackOverflowException means that some code has entered an endless loop. Consider the example below, which will never terminate. Imagine that Main is the entry-point into your application (i.e. Outlook addin), then method A and Main will keep calling each other until an StackOverflowException occurs.

public void Main()
{
    A();
}

public void A()
{
    Main();
}

If you can run the application in debug-mode in Visual Studio, it will generally tell you the stack trace from where the StackOverflowException occurred. Using that information it should be possible to track down the error.

It is often caused by recursive functions (e.g. due to misuse or with a "bad" stopping condition) or events that keep raising each other.

sb.olofsson
  • 899
  • 9
  • 14
0

The most common cause is a recursive function: a function that calls itself.

See this for troubleshooting tips: How do I prevent and/or handle a StackOverflowException?

When you're debugging you want to pause/halt code execution when the call stack grows extremely large and then the problem will be visible. Here is an example, if you called this function with large numbers you'd get a Stackoverflow Exception:

private long Multiply(int x, int y) 
{
  x+=y;
  y--;
  if (y==0) return x;
  return Multiply(x,y);
}
Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321