Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:


C#
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
Posted

You are recursively calling the Fill method without ever checking if you have reached any of the edges of the picture.
 
Share this answer
 
Comments
supernorb 20-Feb-13 9:07am    
I'm sorry but what about the if condition? I've drawn an enclosed line (like a circle) and it should stop recursively calling because the color on the enclosed line is not the same to the back color. Thanks.
CHill60 20-Feb-13 9:31am    
But then using Fill ... p.Y + 1. When you step through the debugger is it that line that throws the exception?
Richard MacCutchan 20-Feb-13 9:50am    
You are also potentially going beyond the edges of your picture. Either way the recursion is the problem and you need to use your debugger to check the actual conditions that exist at the time of the exception.
supernorb 20-Feb-13 10:26am    
If so, it shouldn't work in all cases regardless of the size of the enclosed line, however if I draw the enclosed line small enough, it does work.
Richard MacCutchan 20-Feb-13 10:40am    
No, there is a basic flaw in your code, in that it can recurse too deeply and cause stack overflow. The way to solve this is not just to draw smaller lines, but to change your logic so it can work in all cases.
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
 
Share this answer
 
Comments
supernorb 20-Feb-13 10:25am    
Do you mean this code can't work? In fact if I draw the enclosed line small enough, it works well, however I think this is because of how the Stack in .NET works.
Simon_Whale 20-Feb-13 10:33am    
From what you have shown I think your code has a good chance of running the same co-ords more than once. Thus giving you a larger amount of recursive calls than necessary.

supernorb 20-Feb-13 11:57am    
Thanks, I understand, it's just a test.

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