为什么会导致StackOverflowException? [英] Why does this lead to a StackOverflowException?

查看:100
本文介绍了为什么会导致StackOverflowException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我必须说这个颜色填充算法真的没有效率,但它应该工作,我不明白它为什么会引发StackOverflowException(发生在System.Drawing.dll中),这里是代码:





  private   void 填充(Point p,Color backColor)
{
if (drawing.GetPixel(pX,pY)== backColor)
{
drawing.SetPixel(pX,pY,penColor);
Fill( new Point(p.X + 1 ,p.Y),backColor);
Fill( new Point(p.X,p.Y + 1 ),backColor);
Fill( new Point(p.X - 1 ,p.Y),backColor);
Fill( new Point(p.X,p.Y - 1 ),backColor);
}
}





penColor是我要填充的颜色,粗体线是在哪里IDE突出显示以通知异常。方法Fill在面板的mousedown事件处理程序中调用(当然我已经有一些封闭的线来测试Fill()方法)



你能否请展示为什么代码可以引发StackOverflowException(发生在System.Drawing.dll中)?



我们非常感谢您的帮助!



VipHaLong

解决方案

您递归调用 Fill 方法而不检查是否已到达图片的任何边缘。


根据MSDN文档,当执行堆栈上有太多嵌套调用时会发生此错误



MDSN:Stackoverflow类 [< a href =http://msdn.microsoft.com/en-us/library/system.stackoverflowexception(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]


来自外观的
你的代码每次调用方法然后可以调用相同的方法4次(当然使用不同的值,有些也重叠。)。如果你从1,1开始,你会看到你叫它4次



拨打电话1:2,1

拨打2:1 ,2

拨打3:0,1

拨打4:1,0



然后拨打电话1 :



拨打电话:3,1

电话b:2,2

电话c:1, 1

电话d:2.0



电话2:



电话x:2,2

电话y:1,3

电话z:0,2

电话1:2,1

First of all, I have to say that this Color Filling algorithm is really not efficient at all but it should work, I don''t understand why it can raise a StackOverflowException (occurred in System.Drawing.dll), here is the code:


private void Fill(Point p, Color backColor)
        {
            if (drawing.GetPixel(p.X, p.Y) == backColor)
            {
                drawing.SetPixel(p.X, p.Y, penColor);
                Fill(new Point(p.X + 1, p.Y), backColor);
                Fill(new Point(p.X , p.Y + 1), backColor);
                Fill(new Point(p.X - 1, p.Y), backColor);
                Fill(new Point(p.X , p.Y - 1), backColor);
            }
        }



penColor is the color I want to fill with, the line in bold is where the IDE highlighted to inform the exception. The method Fill is called in a mousedown event handler of a panel (of course I already had some enclosed line to test the Fill() method)

Could you please show me why the code can raise a StackOverflowException (occurs in System.Drawing.dll)?

Your help would be highly appreciated!

VipHaLong

解决方案

You are recursively calling the Fill method without ever checking if you have reached any of the edges of the picture.


According to MSDN documentation this error occurs when there are too many nested calls on the execution stack

MDSN : Stackoverflow class[^]

from looking your code each call to the method then can call the same method 4 times (of course with different values, some overlapping too.). if you start at 1,1 you will see that you call it 4 times

call 1: 2,1
call 2: 1,2
call 3: 0,1
call 4: 1,0

then using call 1:

call a: 3,1
call b: 2,2
call c: 1,1
call d: 2.0

the call 2:

call x: 2,2
call y: 1,3
call z: 0,2
call 1: 2,1


这篇关于为什么会导致StackOverflowException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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