Painters Function

I have made a simple paint program, and eventually I added a fill function, and it does properly fill but it instantly crashes. When I run a debugger, it is saying that std::vector::size() in the getcolor function is failing when I run the code. Here is what I have that is relevant to the issue.

Part of the image class

////////////////////////////////////////////
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
image::image(int width2, int height2)
{
    resize(width2,height2);
    clear(0x00,GR_PIXEL());
}

image::image()
{
    resize(1,1);
    clear(0x00,GR_PIXEL());
}

void image::resize(int width2, int height2)
{
    if(width2>=1 && height2>=1)
    {
    width=width2;
    height=height2;
    pixelset.resize(width2);
    for(int counter=0; counter<width2; counter++)
        pixelset[counter].resize(height2);
    }
else
    resize(1,1);
}

PIXEL image::getpixel(int x, int y)
{
    if(x<getwidth()&&x>=0 &&y<getheight()&&y>=0)
        return pixelset.at(x).at(y);
    else
        return PIXEL(-1,'\0');
}

int image::getwidth(void)
{
    return width;
}

int image::getheight(void)
{
    return height;
}

int image::getcolor(int x, int y)
{
if(x>=0 &&x <getwidth() && y>=0 &&y<getheight())
{
    return getpixel(x,y).getColor();
}
else
    return -1;
}

char image::getimage(int x, int y)
{
if(x>=0 &&x <getwidth() && y>=0 &&y<getheight())
{
        return getpixel(x,y).getImage();
}
else
    return '\0';
}

///////////////////////////////

Here is my fill alghorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Engine::floodFillUtil(int x, int y, int prevC, int newC, char prevI, char newI)
{
    // Base cases
    if (x < 0 || x >= mainimage.getwidth() || y < 0 || y >= mainimage.getheight())
        return;
    if (mainimage.getcolor(x,y) != prevC || mainimage.getimage(x,y)!=prevI)
        return;

    // Replace the color at (x, y)
    mainimage.putpixel(x,y,newC,newI);

    // Recur for north, east, south and west
    floodFillUtil(x+1, y, prevC, newC, prevI, newI);
    floodFillUtil(x-1, y, prevC, newC, prevI, newI);
    floodFillUtil(x, y+1, prevC, newC, prevI, newI);
    floodFillUtil(x, y-1, prevC, newC, prevI, newI);
}

// It mainly finds the previous color on (x, y) and
// calls floodFillUtil()
void Engine::floodFill( int x, int y, int newC, char newimage)
{
    floodFillUtil(x, y, mainimage.getcolor(x,y), newC, mainimage.getimage(x,y),newimage);
}

/////////////////////////////////////////////

I have no idea what is causing this to crash. Please also note that this is for use with ascii characters for text art.
When I run a debugger, it is saying that std::vector::size() in the getcolor function is failing when I run the code.

I'd say you're misinterpreting what's happening there, but you don't supply any way to reproduce the error so it's hard to say how. It's quite possible that line 30 in the first snippet is throwing. "It's failing" isn't much to go on.
Sorry, should have clarified, the debugger said that line 30 is failing, causing my program to crash. The code may be too long so I may just want to post the code on dropbox. It is using mingw with a graphics system I made.
Here is the full code:
https://www.dropbox.com/s/h66gcf2rf3yyz4v/AEARTCreator.zip?dl=0
Last edited on
Here i the debugging info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#0 0040E48E	std::vector<std::vector<PIXEL, std::allocator<PIXEL> >, std::allocator<std::vector<PIXEL, std::allocator<PIXEL> > > >::_M_range_check(this=0x28ff00, __n=24) (c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:793)
#1 0040FCDD	std::vector<std::vector<PIXEL, std::allocator<PIXEL> >, std::allocator<std::vector<PIXEL, std::allocator<PIXEL> > > >::at(this=0x28ff00, __n=24) (c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:812)
#2 00401C55	image::getpixel(this=0x28ff00, x=24, y=15) (F:/projects/AEARTCreator/engine/AEART/image.cpp:36)
#3 00401E4C	image::getcolor(this=0x28ff00, x=24, y=15) (F:/projects/AEARTCreator/engine/AEART/image.cpp:71)
#4 00406B1A	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:199)
#5 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#6 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#7 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#8 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#9 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#10 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#11 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#12 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#13 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#14 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#15 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#16 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#17 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#18 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#19 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#20 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#21 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#22 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#23 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#24 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#25 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#26 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#27 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206)
#28 00406BFE	Engine::floodFillUtil(this=0x28fd78, x=24, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:207)
#29 00406BC3	Engine::floodFillUtil(this=0x28fd78, x=23, y=15, prevC=3, newC=3, prevI=-37 'Û', newI=-37 'Û') (F:/projects/AEARTCreator/engine/engine.cpp:206) 



It crashes whenever you select the bucket tool by pressing B and clicking on the image.
Last edited on
..._M_range_check...
...at(this=0x28ff00, __n=24)...

...image::getpixel...


You are feeding an index outside the valid range to one of those at's on line 30.
I know, i figured that was the issue, but I have at line 29, a check to make sure it isnt. I did notice something though, i put in a Sleep(1) at line 8 of the second snippet, and it doesn't crash? It is completely confusing me. Any ideas?
I know, i figured that was the issue, but I have at line 29, a check to make sure it isnt.

But you don't. You're checking the height and width members of the class. Not the size of the containers you're accessing.

I did notice something though, i put in a Sleep(1) at line 8 of the second snippet, and it doesn't crash?

Sometimes if making a function call changes things it can be stack corruption that is the culprit (since it often changes the way the stack is used.)
Also, the width and height are set in the resize function
It is probably stack corruption.
Topic archived. No new replies allowed.