Segmentation fault gettin witdth or height from SDL_Surface*

This gives me a segmentation fault on the last line. why is that? picture loaded would be a .png

Sprite::Sprite(int i, int x, int y, int w, int h, std::string filename):id(i){
SDL_Surface* picture = IMG_Load(filename.c_str());
SDL_Rect* hitBox = new SDL_Rect();
hitBox->x = x;
hitBox->y = y;
hitBox->w = picture->w;
}

Funny thing is that Eclipse would run this while a debugger such as gdb wont. Eclipse can even print picture->w to std::cout and state the correct value.

Is this a bug? cant i get the width from the image?

Use this:

SDL_Rect *hitBox = new SDL_Rect;

Even better, if you aren’t returning the SDL_Rect you can just declare it
like this:

SDL_Rect hitBox;

hitBox.x = x;

hitBox.y = y;From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of hankmander
Sent: Tuesday, January 26, 2010 10:47 AM
To: sdl at lists.libsdl.org
Subject: [SDL] Segmentation fault gettin witdth or height from SDL_Surface*

This gives me a segmentation fault on the last line. why is that? picture
loaded would be a .png

Sprite::Sprite(int i, int x, int y, int w, int h, std::string
filename):id(i){
SDL_Surface* picture = IMG_Load(filename.c_str());
SDL_Rect* hitBox = new SDL_Rect();
hitBox->x = x;
hitBox->y = y;
hitBox->w = picture->w;
}

Funny thing is that Eclipse would run this while a debugger such as gdb
wont. Eclipse can even print picture->w to std::cout and state the correct
value.

Is this a bug? cant i get the width from the image?

I think it’s pretty certain your call to IMG_Load is returning a NULL pointer. It may be that Eclipse runs it correctly because of certain differences in the target “run” location, while your debugger defaults to a different “run” location. In either case, you ought to include code such as

if (!picture)
{
//give error message & slap in the face
}

Checking for the success of your IMG_Load calls is always a good idea.
-DaveFrom: hankmander
Sent: Tuesday, January 26, 2010 9:47 AM
To: sdl at lists.libsdl.org
Subject: [SDL] Segmentation fault gettin witdth or height from SDL_Surface*

This gives me a segmentation fault on the last line. why is that? picture loaded would be a .png

Sprite::Sprite(int i, int x, int y, int w, int h, std::string filename):id(i){
SDL_Surface* picture = IMG_Load(filename.c_str());
SDL_Rect* hitBox = new SDL_Rect();
hitBox->x = x;
hitBox->y = y;
hitBox->w = picture->w;
}

Funny thing is that Eclipse would run this while a debugger such as gdb wont. Eclipse can even print picture->w to std::cout and state the correct value.

Is this a bug? cant i get the width from the image?



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org