Very odd SDL error

Started by
10 comments, last by Grant1219 17 years, 9 months ago
Ahh, there we go...

Well, at first I thought GetImage() is the error because I thought that it actually loads an image from the harddisk each time it is called. But it doesn't - it returns a local image.

In that case, you are caling SDL_FreeSurface() on that image, which is local to CGraphics. The first time it is ok, but the second time the image is already deleted. So from here:
void CTile::MakeTile(string id, int xpos, int ypos, int type){/*    this function is where acces violation happens*/    ID = id;    SDL_Surface *Tile;    Tile = TileImage.GetImage(ID);    TileRect.w = Tile->w;    TileRect.h = Tile->h;    TileRect.x = xpos;    TileRect.y = ypos;    Type = type;    SDL_FreeSurface(Tile);}

Take out the red part (SDL_FreeSurface(Tile);).

Now CGraphics should be the one who "frees" the surface, because it owns it (otherwise it turns into a memory leak).

Cheers, and good luck!

Note: Double underscores in the front of an identifier are a big no-no - they are reserved for the compiler and preprocessor. Instead, go with something like this:
#ifndef HEADER_NAME_H#define HEADER_NAME_H...#endif /* HEADER_NAME_H */
Advertisement
I tried everything you told me to try, but I am still getting this access violation. I tried commenting out parts of the MakeTile function, and found that with even "ID = id;" being the only thing it that function, it still gives the access violation. This makes no sense to me at all, is there anything else I could try? Maybe I should just post the rest of the source.

Also thanks for your help so far.

This topic is closed to new replies.

Advertisement