Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+ WinForm
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
Posted 20-Feb-13 3:34am
supernorb2.6K
Rate this: bad
good
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.
  Permalink  
Comments
supernorb at 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 at 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 at 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 at 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 at 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 at 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 at 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
good
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
  Permalink  
Comments
supernorb at 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 at 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 at 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
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
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