seg fault when copying SDL_Surface

Started by
8 comments, last by kev000 16 years, 11 months ago
What would cause my program to crash when I copy this surface: "mysurface = s;"? ... private: SDL_Surface *mysurface; }; void surfaceClass::init(SDL_Surface *s, SDL_Rect a, int nalpha) { if(s==NULL) { //sdlnerror::errorLoading( "Error loading surface, blitting disabled." ); visibility=0; mysurface=NULL; } else { mysurface = s; //Crashes here!!! myarea.w = mysurface->w; myarea.h = mysurface->h; } } Anyone have any ideas why it would crash at this point?
Advertisement
Since it's happening inside a member function and when accessing any members of the same class, I believe you are swimming into some undefined behavior. If you are calling it from a pointer to that class, check if that pointer is valid.


BTW, proper way to add references to a SDL_Surface is by doing:
SDL_Surface *s1,*s2;s2 = get_surface_from_somewhere();s1 = s2;s1->refcount++;  // you add this


So, when freeing (it will reduce number of refs and, if zero, will free it):
SDL_FreeSurface(s1);SDL_FreeSurface(s2);  // this would get a segfault, but no more


If you just want to copy content, create a new surface with same format and blit one onto the other.
Are you absolutely certain it crashes on exactly that line? How did you determine that?
I'm sure it crashes there, at least that's where the debugger stops.
Then you're probably calling the member function on an uninitialised or otherwise invalid pointer to a surfaceClass instance, as mentioned above.
SOLVED! Big thanks guys, the surfaceClass object I created was a pointer (surfaceClass *surface) and that was causing the crash. I just initialized it normal and it worked (surfaceClass surface).

quick off topic question:
how do I load a png image (using SDL_Image) that has transparencies and get it to display properly?
It should "just work". If it doesn't, explain what exactly you're doing, what you see, and what you expected instead.
Here's the code:

int buttonclass::create(int x, int y, string srcfile, string n, SDL_Surface *target, TTF_Font *f,SDL_Color c)
{
[snip]
SDL_Surface* loadedImage = NULL, *tempsurf = NULL;
loadedImage = IMG_Load( srcfile.c_str() );
if( loadedImage != NULL )
{
tempsurf=SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
else
{
sdlnerror::errorCreatingButton( srcfile.c_str() );
return -1;
}
SDL_SetAlpha( tempsurf, SDL_RLEACCEL | SDL_SRCALPHA, 255);
mysurface.init(tempsurf,area,0);
[snip]
alphavalue=200;
mysurface.setalpha(200);
ndebug::out("buttonclass::create() finished!",2);
return 1;
}

When it's loaded up into surfaceClass, it will then be blitted directly to the screen.

here is a link to what it looks like now:
http://www.nextabyte.com/nanonymous/forum/files/screenshot_202.png

here is a link to what it is supposed to look like:
http://www.linuxice.com/pngfinals/Main.png

Seems to me that you are blitting the entire image to the background with alpha = 200. Alpha should vary by each pixel, so you can get the same effect you see on the other image.
I guess my question would be how do I activate the per-pixel alpha? Is there a function that does this or should it work automatically? I have tried blitting the image to the background without any SetAlpha() being done and I get the same results. Is there something I have to do to the target surface before I blit it?

This topic is closed to new replies.

Advertisement