Crash couse SDL_GetRGB()


Hi there, fellows!
Iv try to check bitmap for pixels color using this code:

SDL_Color WrongPixelsFinder::get_pixelColor(SDL_Surface *surface, int x, int y)
SDL_Color theKey = { -1, -1, -1, -1 };
if (x < 0 || y< 0 || y > surface->h || x > surface->w)
return theKey;
Uint32 pixels = (Uint32)surface->pixels;
SDL_GetRGB(pixels[(y * surface->w) + x], surface->format, &theKey.r, &theKey.g, &theKey.b);
return theKey;

Code where i used this method:


comprColors is just function where i compere, like if(rgb.b == rgb1.b) return true

After some time, loop got crash and that kind of error shows up:
“Unhandled exception at 0x01056F0E in PixelFinder.exe: 0xC0000005: Access violation reading location 0x7EFD3000.”

Anyone know why?


If you have an image that is 640x480, and a y of 480, you get this: pixels[(480 * 640) + x]. No matter what x is, you will be past the memory limit allocated for the surface. If you ensure that y is never equal to the height of the surface, it should work. y >= surface->h

if (comprColors(get_pixelColor(surface, x, y), get_pixelColor(surface, x, (y + 1))) == false && comprColors(get_pixelColor(surface, x, y), get_pixelColor(surface, x, (y - 1))) == false && comprColors(get_pixelColor(surface, x, y), get_pixelColor(surface, (x + 1), y)) == false && comprColors(get_pixelColor(surface, x, y), get_pixelColor(surface, (x - 1), y)) == false)

In that if statement, you’re adding 1 to y and later x. Your for loops are under the bounds of the arrays, but when the if statement is hit, it’s going out of bounds and causing your segfault.


Okay, so iv added those changes to my code. “y >= surface->h” to if at get_pixelColor, “y<= surface->h” at loop, but it still doesn’t work.

Program checked image 2048x2048, reached 1561 for y, and 1022 for x. After this, its got crash, always in the same time.

Its so weird, don’t get it why.


I just looked at the second part of your Paste ofCode again and noticed this:

if (maxW_PX < 1000)
    w_pixel[maxW_PX - 1].x = x;
    w_pixel[maxW_PX - 1].y = y;
    DAMChanger(&w_pixel, maxW_PX, 1, 1);
    maxW_PX += 1;

I’m not sure where maxW_PX comes into play, but if it’s ever 0, then the program might be crashing there. 0 - 1 will result in a negative array position, so it’s possible that you’re trying to access memory that is outside of the bounds of your array.
Are negative array indexes allowed in C? Second answer.

If that doesn’t help, then how do you know when the program crashes? Are you using a debugger and watching the variables to know where it’s stopping or are you printf debugging some more? //cout << "Znaleziono" << endl; I saw that in your code, and if you’re printf debugging, remember to flush stdout after each time you print to it. I personally prefer that to normal debuggers; but I’m a glutton for punishment.


No, no. maxW_PX started with default value 1.

I Just start in debug, not realese. When its crashed, got message and place where it crashed.
Visual points at this SDL_GetRGB(pixels[(y * surface->w) + x], surface->format, &theKey.r, &theKey.g, &theKey.b);

I dont know, maybe its DAMChanger fault…Iv made it, and im not a profesional.


Iv made it, and im not a profesional.
Neither am I. In fact, I failed the course I took in college for C and never took any C++ courses at all; doesn’t mean you can’t learn or use them.

Your DAMChanger function and how you’re handling the memory is a bit inefficient as you’re constantly reallocating memory and the heap could be getting cluttered because of it. It’s also doing the same thing for mode no matter what it is. Issues for another time though.

Now for the current issue, the 1561 and 1022 that you see when it crashes, where and when do you get those numbers? Are you reading them from your debugger or somewhere else? Your debugger says that it’s crashing at that SDL_GetRGB, so it’s trying to access that surface and running out of bounds for some reason.

Give this a try: download Dr. Memory and run your program using it to see where your memory issue is. It’s like Valgrind for Windows.