Click here to Skip to main content
13,556,249 members
Rate this:
Please Sign up or sign in to 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:

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!

Posted 20-Feb-13 2:34am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are recursively calling the Fill method without ever checking if you have reached any of the edges of the picture.
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.
supernorb 20-Feb-13 12:00pm
Thanks, by the way, do you know the size of stack designed in .NET? I can understand my code produces too many entries in stack (most of the entries are the same calls, which is not necessary).
Richard MacCutchan 20-Feb-13 12:48pm
Sorry, I don't know the stack size, but I'm sure a Google or MSDN search would locate it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04-2016 | 2.8.180515.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100