Question

This is my very first question:

First of these 2 functions you see here below works fine to some extent:

Uint32 AWSprite::get_pixelColor_location(SDL_Surface * surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch (bpp) {
case 1:
    return *p;
case 2:
    return *(Uint16 *)p;
case 3:
    if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
        return p[0] << 16 | p[1] << 8 | p[2];
    else
        return p[0] | p[1] << 8 | p[2] << 16;
case 4:
    return *(Uint32 *)p;
default:
    return 0;       
}

}

void AWSprite::set_all_frame_image_actual_size() {
/* This function finds an entire rows that has transparency
   then stores the amount of rows to a Frame_image_absolute structure
*/
absolute_sprite = new Frame_image_absolute*[howManyFrames];
for (int f = 0; f < howManyFrames; f++) {
    SDL_LockSurface(frames[f]);
    int top_gap = 0; int bottom_gap = 0;
    int per_transparent_px_count = 1;
    for (int i = 0; i < frames[f]->h; i++) {
        int per_transparent_px_count = 1;
            if (this->get_pixelColor_location(frames[f], j, i) == transparentColour) per_transparent_px_count++;
            if (per_transparent_px_count >= frames[f]->w) {
                if (i < frames[f]->h / 2) {
                    per_transparent_px_count = 1;
                    top_gap++; 
                } else {
                    per_transparent_px_count = 1;
                    bottom_gap++;
                }
            }
        }
    }
    int realHeight = frames[f]->h - (top_gap + bottom_gap);
    absolute_sprite[f] = new Frame_image_absolute();
    absolute_sprite[f]->offset_y = top_gap;
    absolute_sprite[f]->height = realHeight;
}

}

When i ran this i get: Unhandled exception at 0x00173746 in SE Game.exe: 0xC0000005: Access violation reading location 0x03acc0b8.

When i when through debuging, i found that it crashes at: When iterators variable f == 31, i == 38, j = 139 And stops at AWSprite::get_pixelColor_location() in the line at " return *(Uint32 *)p;

I found that if i ran it again and go through debugging line by line then i will works sometime and sometime it dont!!! So i mean that "It crash at randomly when f > 30, i, j iterators value"

What is going on...

Was it helpful?

Solution

I cannot comment on the question yet, but here are some questions:

Where does j come from? Based on the get_pixelColor_location function I would assume that you're iterating over the width of the surface. This part seems to be missing from the code you posted.

Did you validate that i and j are within the bounds of your surface?

Also, you don't seem to Unlock the surface.

Running your function seems to work adequately here so I suspect you're reading outside of your buffer with invalid parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top