堆栈溢出异常如何解决 [英] stack over flow exception how to resolve

查看:159
本文介绍了堆栈溢出异常如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在 Lista.add()的代码和平运行时遇到堆栈溢出异常。

我无法确定原因。


  if (dt.Rows.Count >   0 
{
lista.Clear();
for int i = 0 ; i < dt.Rows.Count; i ++)
{
lista.Add( new 库存(dt.Rows [i] [ ID]。ToString( ),dt.Rows [i] [ Serial_No]。ToString(),dt.Rows [ i] [ Part_Code]。ToString(),dt.Rows [i] [ 日期]。ToString(),dt.Rows [i] [ 数量]。ToString(),dt.Rows [i] [ 类别]。ToString()));
}





帮助或suggesstion将不胜感激



谢谢

解决方案

StackOverflowException 由一个创建深的递归方法触发调用堆栈。这个问题通常与堆栈内存区域的概念有关。



基础知识:来自维基百科 [ ^ ]

当堆栈指针超出堆栈限制时,会发生堆栈溢出。调用堆栈可以包括有限数量的地址空间,通常在程序开始时确定。调用堆栈的大小取决于许多因素,包括编程语言,机器架构,多线程和可用内存量。当程序试图使用比调用堆栈上可用空间更多的空间时(也就是说,当它试图访问超出调用堆栈边界的内存时,实际上是缓冲区溢出),堆栈被称为溢出,通常导致程序崩溃。





如何解决:从MSDN [ ^ ]

从.NET Framework 2.0版开始, StackOverflowException object 无法被try-catch块捕获,默认情况下终止相应的进程。因此,建议用户编写代码以检测并防止堆栈溢出。例如,如果您的应用程序依赖于递归,请使用计数器或状态条件来终止递归循环。





我建议您调试程序。让它抛出异常。抛出异常后再查看堆栈跟踪。堆栈跟踪将具有一个模式,显示任意数量的重复方法调用。弄清楚哪个方法调用不应该在堆栈跟踪中调用另一个方法调用,并且您的错误将消失。如果您无法在堆栈跟踪中找到该模式,只需单击改善问题并提供堆栈跟踪。我们将为您提供帮助。





这里我添加了一个链接,可以帮助您读取堆栈跟踪。

Step by跟踪初学者ASP.NET应用程序的步骤指南 [ ^ ]



- Amit

Hi,

I am getting stack over flow exception when running below peace of code at Lista.add().
I am not able to identify the cause.

if (dt.Rows.Count > 0)
               {
                   lista.Clear();
                   for (int i = 0; i < dt.Rows.Count; i++)
                   {
                           lista.Add(new Inventory(dt.Rows[i]["ID"].ToString(),dt.Rows[i]["Serial_No"].ToString(), dt.Rows[i]["Part_Code"].ToString(), dt.Rows[i]["Date"].ToString(), dt.Rows[i]["Quantity"].ToString(), dt.Rows[i]["Category"].ToString()));
                   }



help or suggesstion would be appreciated

Thanks

解决方案

The StackOverflowException is triggered by a recursive method that creates a deep call stack. The problem is linked to the concept of the stack memory region in general.

Basics : From Wikipedia[^]

Stack Overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.



How to Resolve: From MSDN[^]

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.



I would suggest you to debug the program. Let it throw the exception. Once 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 are unable to find the pattern in your stack trace, just click on Improve question and provide the stack trace. We will help you on that.

[Edit 1]
Here I am adding a link which will help you to read stack trace.
Step by Step Guide to Trace the ASP.NET Application for Beginners[^]

--Amit


这篇关于堆栈溢出异常如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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