积极答复者
System.StackOverflowException类型的未经处理的异常

问题
-
Public Structure Direction Public lypx As Integer Public lypy As Integer Public Sub New(ByVal _lypX As Integer, ByVal _lypY As Integer) lypx = _lypX : lypy = _lypY End Sub End Structure Sub PaintBucket(ByVal x As Integer, ByVal y As Integer, ByVal MyBitmap As Bitmap) Dim lypColor As Color = ColorDialog1.Color Dim NewColor As Color = Color.Black Dim i As Integer Dim lypDirection() As Direction = {New Direction(-1, 0), New Direction(0, 1), New Direction(1, 0), New Direction(0, -1)} If x < 472 And x > 0 And y > 0 And y < 477 Then If MyBitmap.GetPixel(x, y).R <> 255 Then MyBitmap.SetPixel(x, y, Color.Yellow) 'MsgBox("lyp") For i = 0 To 3 Step 1 PaintBucket(x + lypDirection(i).lypx, y + lypDirection(i).lypy, MyBitmap) Next End If End If End Sub
为什么会System.StackOverflowException??
Visual Basic 初学者 望关照!
答案
-
可能你需要调试代码,尤其仔细观察当0 到 3的循环的时候,x究竟怎么变化而无法脱出循环导致死循环递归的。
- 已标记为答案 LYP VB Learner 2013年2月24日 11:16
全部回复
-
PaintBucket
这方法明显使用了递归效果。总是不满足退出递推条件,故而发生死循环导致内存溢出。
-
Sub PaintBucket(ByVal x As Integer, ByVal y As Integer, ByVal MyBitmap As Bitmap) Dim lypColor As Color = ColorDialog1.Color Dim NewColor As Color = Color.Black Dim i As Integer Dim lypDirection() As Direction = {New Direction(-1, 0), New Direction(0, 1), New Direction(1, 0)} ', New Direction(0, -1)} If x < 472 And x > 0 And y > 0 And y < 477 Then If MyBitmap.GetPixel(x, y).R <> 255 Then MyBitmap.SetPixel(x, y, Color.Yellow) 'MsgBox("lyp") For i = 0 To 2 Step 1 PaintBucket(x + lypDirection(i).lypx, y + lypDirection(i).lypy, MyBitmap) Next End If End If End Sub
可是为什么将代码改成这样就可以正常运行了??Visual Basic 初学者 望关照!
-
PaintBucket(x + lypDirection(i).lypx, y + lypDirection(i).lypy, MyBitmap)
因为你传入的是x+……内容,总有一次会不满足x<472的条件(退出循环)。不会继续递归。
如果总是不满足,照样死循环。
-
不是很明白,,能给我一个解决方案吗??谢谢了
设法改成你第二种形式的代码(x+……)就可以了。
Visual Basic 初学者 望关照!
-
可是两种形式都是x+...啊...
Visual Basic 初学者 望关照!
对不起,看快了……
貌似你的代码最大差别在于一个For——前者循环4次,后者3次。
你内存多少大?会不会是过于耗费内存导致溢出的?照例而言代码貌似没有错。
-
可能你需要调试代码,尤其仔细观察当0 到 3的循环的时候,x究竟怎么变化而无法脱出循环导致死循环递归的。
- 已标记为答案 LYP VB Learner 2013年2月24日 11:16