Click here to Skip to main content
15,891,816 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have restarted a huge project twice because of this error and development has been going fine until I came to turn on my computer again today when I could not build the project and got the StackOverflowException. I cannot do anything or visual studio will restart. It has been fine and this error has come up out of nowhere in both projects. Here is part of the code:

C#
private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("To get data click getdata");
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Data Tree", new System.Windows.Forms.TreeNode[] {
            treeNode1});
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BASE));
            this.baseContainer = new System.Windows.Forms.Panel();
            this.headerContainer = new System.Windows.Forms.Panel();
            this.menuPage = new System.Windows.Forms.Button();
            this.dataSource = new System.Windows.Forms.PictureBox();
            this.entryName = new System.Windows.Forms.Label();
            this.shapeContainer3 = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
            this.headerDivider = new Microsoft.VisualBasic.PowerPacks.LineShape();
            this.pageContainer = new System.Windows.Forms.Panel();

The error is on this line:
C#
this.baseContainer = new System.Windows.Forms.Panel();

Error details: An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

It is in the .Designer.cs file for the form and I just do not know what to do and I have done nothing wrong. I am using Visual C# 2010 Express, Thank you

Edit: After //ing out that line another one throws but is not the next line, it is the shapeContainer3 line
Posted
Updated 4-Oct-13 4:49am
v3
Comments
Pheonyx 4-Oct-13 10:48am    
Have you checked your computers memory usage when this error occurs?
What version of Visual Studio are you using?
What OS are you running? Is it 32 or 64 bit ?
Henry Hunt 4-Oct-13 10:54am    
- I can't see any memory usage problems and I don't have much open

Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.5.50709 SP1Rel
Installed Version: C# Express
- Windows 8 64bit
Pheonyx 4-Oct-13 10:58am    
Could you build the project yesterday last thing before you left?
Henry Hunt 4-Oct-13 11:01am    
Yes, completely fine. Then I saved and closed and turned off my computer. Had my day and come back to find it starts to throw errors
Sandesh M Patil 4-Oct-13 11:07am    
Clean the solution and rebuild it

1 solution

The error isn't necessarily on this line: this is just where the exception happens to be thrown when you try to push something on the execution stack and it is already full. You might even hit this line every time, but it isn't likely to be the cause. What is happening is pretty much either going to be infinite recursion. It is worth reviewing how this exception occurs, with a simplified version of what is happening. Imagine the following code:

C#
public void Foo()
{
  MethodA(); //Assume no recursion
  Foo(); //Infinite recursion
}


Now imagine the call stack has a maximum of 3 items (in reality, much larger than this - typically it will take a few seconds to fill).

  1. The first item is the original call to Foo() Stack Items = 1
  2. The first call to MethodA() pushes one to to the stack Stack Items = 2
  3. MethodA's implementation (not shown - it doesn't matter) always happens to push one item to the stack Stack Items = 3, this completes popping it back off Stack Items =2 As MethodA completes, it is popped off the stack also Stack Items =1
  4. Now the recursive call to Foo() is made, it is added to the stack, stack items = 2
  5. The second call to MethodA() pushes one to to the stack Stack Items = 3
  6. MethodA's implementation again tries to add one to the stack but the stack is full. The exception is thrown from this point in MethodA


So you've a situation that the error is thrown inside MethodA but the root cause is actually Foo calling itself without breaking out. Worse, the exception is effected by the size of the stack before the loop is executed and any difference to the stack (say due to conditional statements) inside the loop - you can get the exception potentially anywhere inside the loop. This makes this exception hard to trace sometimes.

The first thing you need to do is look at the exception stack trace. If you are lucky and you'll see the loop happening, blocks of trace repeating over and over. In this case, you just need to find where in your code the break-out should happen and add code to break the loop. If you don't see your code anywhere then you are going to have to find the loop manually: to do this place breakpoints a different places you know are being executed when the exception happens (this can be in the initialisation code of a control don't forget) and see which are hit repeatedly. This should find your infinite loop hopefully.

Sorry this is such an essay!


[Edit - in repsonse to the OP's Comment]
Not sure whether I was clear about this, the stack cam fill anywhere in code executed inside the loop. the exception might be thrown in the designer code, but the loop will be happening around it. E.g. you have an infinitely recursive on a page, inside the loop a child control is created, the exception could happen in the child control that gets created (including any designer code that gets run, or anywhere else that happens to be excuted as part of the loop for that matter).
You need to look at the stack-trace when debugging. There will be stack trace on the exception (other than the displayed one on the YSOD, which is truncated IIRC there is a stack trace property that you can examine http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^])
Inside the stack trace is a list (OK stack :) ) of every method that was currently executing when the exception happened (Most recent at the top). In my example code you get the following order in the stack:


  • ChildMethodOfA()<---Exception here
  • MethodA()
  • Foo() <--- Recursion here
  • Foo()

Normally it isn't as clean as this, making getting to the bottom of the problem a bit of a dark art sometimes.You need to pick through the stack trace to spot where the loop happening, you'll see the same calls repeated inside the stacktrace, this should help you find useful breakpoints in your own code. With luck you'll be able to spot it quickly. If not, it becomes educated guesswork. You could put a breakpoint in the designer code and try and step through, eventually you'll see where the loop is happening in your code. If this doesn't work out, you need to decide make an attempt to find out what is executing the designer code when is throwing the exception. Again it it is a case of breakpoint --> step through until you see the recursion.

Note that you might [or even probably- this is easy to spot ] will find you have deeper recursion that isn't just MethodA calling itself, but is called by one or more intermediary methods.
 
Share this answer
 
v2
Comments
Rob Philpott 4-Oct-13 11:44am    
Out of curiosity, how is an infinite loop (non-recursive) likely to blow the stack? I can't think of anyway of doing that.
Keith Barrow 5-Oct-13 3:28am    
Now you come to mention it, I think you are right: Any methods called in the loop would pop off again (unless it called itself recursively). Possibly it could happen if calling in parallel? I'm really not sure what happens to the stack in parallelism, I'd expect there to be problems with the threading first. Might give it a check out!
Henry Hunt 4-Oct-13 14:43pm    
Thank you for this, it helped but the code with the error is always in the automatically generated code by the designer to store all of the properties for the controls I have on the form and not code that I have written so I really don't know how to solve it.
Keith Barrow 5-Oct-13 4:01am    
I've added to my answer. the tldr; version is that you should look at the stack trace to find the recursion in the non-MS code, if that fails then it's educated guesswork....
BillWoodruff 5-Oct-13 6:01am    
That's an excellent response, Keith, and I hope you'll keep on essaying :)

thanks, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900