StackOverflowException请帮忙 [英] StackOverflowException please help

查看:53
本文介绍了StackOverflowException请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重启了一个巨大的项目两次,因为这个错误并且开发一直很好,直到我今天再次打开我的计算机时,我无法构建项目并获得StackOverflowException。我无法做任何事情或视觉工作室将重新启动。一切都很好,这两个项目都出现了这个错误。以下是代码的一部分:



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:

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:

this.baseContainer = new System.Windows.Forms.Panel();



错误详情:未处理的异常在System.Windows.Forms.dll中发生'System.StackOverflowException'类型



它是在.Designer.cs文件中的表单,我只是不知道是什么要做,我没有做错任何事。我正在使用Visual C#2010 Express,谢谢



编辑:在输出该行后,另一行抛出但不是下一行,它是shapeContainer3行


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

After //ing out that line another one throws but is not the next line, it is the shapeContainer3 line

推荐答案

错误不一定在这一行:这就是当你尝试推送某些东西时抛出异常发生的地方在执行堆栈上它已经满了。你甚至可能每次都会遇到这条线,但这不太可能是原因。发生的事情几乎要么是无限递归。值得回顾一下这个异常是如何发生的,用简化的版本来发生。想象一下下面的代码:



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:

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





现在假设调用堆栈最多有3个项目(实际上,比这大得多 - 通常需要几秒钟才能填充)。



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. 第一项是对 Foo()的原始调用 Stack Items = 1
  2. 第一次调用 MethodA()将一个推送到堆栈 Stack Items = 2
  3. MethodA 的实现(未显示 - 无关紧要)总是将一个项目推送到堆栈 Stack Items = 3 ,这样就完成了将它弹回 Stack Items = 2 MethodA 完成时,它也会从堆栈中弹出 Stack Items = 1
  4. 现在递归调用 Foo(),它被添加到堆栈中,堆栈items = 2
  5. 第二次调用 MethodA()将一个推送到堆栈 Stack Items = 3
  6. MethodA 的实现再次尝试将一个添加到堆栈但堆栈已满。 MethodA


  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


$中抛出异常b $ b

所以你遇到的错误是在 MethodA 里面引发的,但根本原因实际上是 Foo 自行调用而不会爆发。更糟糕的是,异常受到循环执行前堆栈大小的影响以及循环内堆栈的任何差异(比如由于条件语句) - 您可以在循环内的任何位置获得异常。这使得这个异常有时难以追踪。



您需要做的第一件事就是查看异常堆栈跟踪。如果你很幸运,你将看到循环发生,一遍又一遍地重复。在这种情况下,您只需要在代码中找到突破发生的位置并添加代码来打破循环。如果你没有在任何地方看到你的代码,那么你将不得不手动找到循环:为此,将断点放在你知道异常发生时正在执行的不同位置(这可以在控件的初始化代码中别忘了,看看哪些是反复击中的。这应该有希望找到你的无限循环。



对不起,这是一篇文章!







不确定我是否清楚这一点,堆栈凸轮填充循环内执行的代码中的任何位置。设计器代码中可能会抛出异常,但循环将围绕它发生。例如。你在页面上有一个无限递归,在循环中创建一个子控件,异常可能发生在创建的子控件中(包括任何运行的设计器代码,或者其他任何碰巧作为其中一部分被执行的代码)循环就此而言。

调试时需要查看堆栈跟踪。异常上会有堆栈跟踪(除了YSOD上显示的那个,这是截断的IIRC,有一个堆栈跟踪属性,你可以检查http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx [ ^ ])

堆栈内部跟踪是异常发生时当前正在执行的每个方法的列表(OK堆栈:))(最近的顶部)。在我的示例代码中,您在堆栈中获得以下顺序:





  • ChildMethodOfA()< - -Exception here
  • MethodA()
  • Foo()< ---此处递归
  • Foo()


  • 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!



    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()

    • 这篇关于StackOverflowException请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆