Sdl freesurface causing seg faults when blitting

I’ve run into my fair share of seg faults while blitting and gotten around them, but this has got me hitting my head against the wall. i’ll I am trying to do is swap one surface with new one. The order I am calling it is changeSurface(); then drawRect(); Any help would greatly be appreciated.

class surfaceID
{
public:
surfaceID(){s=NULL;name="";}

SDL_Surface *s;

};

std::vector surfaces;

int gfxengine_sdl::changeSurface(int &id,SDL_Surface *newsurf)
{
if(newsurf==NULL)return -1;
if(id==-1)
{

}
if(surfaces[id].s!=NULL)
{

  •    SDL_FreeSurface(surface[id].s); 
    
  •    //SDL_FreeSurface(surface[id].s);  ///This by passes the seg fault, but then I have memory leakage :(
    
    }
    surfaces[id].s=newsurf;
    return 1;
    }

int gfxengine_sdl::drawRect(int id)
{
if(id==-1)return -1;
if(surfaces[id].s==NULL) return -1;
SDL_BlitSurface(surfaces[id].s,NULL,screen,&surfaces[id].coords); //seg faults here!!!
return 1;
}

Kevron Rees wrote:

I’ve run into my fair share of seg faults while blitting and gotten around them, but this has got me hitting my head against the wall. i’ll I am trying to do is swap one surface with new one. The order I am calling it is changeSurface(); then drawRect(); Any help would greatly be appreciated.

class surfaceID
{
public:
surfaceID(){s=NULL;name="";}

SDL_Surface *s;

};

std::vector surfaces;

int gfxengine_sdl::changeSurface(int &id,SDL_Surface *newsurf)
{
if(newsurf==NULL)return -1;
if(id==-1)
{

}
if(surfaces[id].s!=NULL)
{

  •    SDL_FreeSurface(surface[id].s); 
    
  •    //SDL_FreeSurface(surface[id].s);  ///This by passes the seg fault, but then I have memory leakage :(
    
    }

Your diffs refer to ‘surface[id].s’ but your vector is named surface’s’.

surfaces[id].s=newsurf;
return 1;

}

int gfxengine_sdl::drawRect(int id)
{
if(id==-1)return -1;
if(surfaces[id].s==NULL) return -1;
SDL_BlitSurface(surfaces[id].s,NULL,screen,&surfaces[id].coords); //seg faults here!!!
return 1;
}
Are you certain ‘&surface[id].coords’ is resolving as you desire?
???>


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

int
gfxengine_sdl::drawRect(int
id)

{

if(id==-1)return
-1;

if(surfaces[id].s==NULL)
return
-1;

SDL_BlitSurface(surfaces[id].s,NULL,screen,&surfaces[id].coords);

//seg
faults
here!!!

return
1;

}

Are
you
certain
’&surface[id].coords’
is
resolving
as
you
desire?
???

yes, i thought that might be the issue also so I made coords.w and coords.h match the surface->w and h. What gets me is the only change I have to make is to comment out the SDL_FreeSurface() line and bingo! it works.

SDL_BlitSurface(surfaces[id].s,NULL,screen,&surfaces[id].coords);        //seg faults here!!!

Of course a seg fault is caused by illegal access to memory, and this
line includes 4 pointers … one of them is invalid.

NULL can be ruled out, so the trick would be to break out the other
three pointers onto separate lines.

You could try:
SDL_BlitSurface(surfaces[id].s,
NULL,
screen,
&surfaces[id].coords);

and possibly get the segfault to identify the one it doesn’t like. If
your compiler optomizes away the distinction, you could try:

SDL_Rect tempRect;
= &surfaces[id].coords;

SDL_Surface *tempSurface = surfaces[id].s;

tempRect.x = tempRect.y = 0;
temprect.w = surfaces[id].coords.w // If it blows up here, there’s
//something wrong with &surfaces[id].coords
tempRect.h = surfaces[id].coords.h;

if ((tempSurface->w != tempRect.w) ||
(tempSurface->h != tempRect.h))
{
cerr << “Gotcha, bad pointer : surfaces[id].s”;
exit (-1);
}

SDL_BlitSurface(tempSurface,NULL,screen,&tempRect);===========

It’s not intended to be production quality code, just explicit access to
your pointers on separate lines to see which one is segfaulting.

If it still segfaults while blitting, check ‘screen’.

CWC wrote:

= &surfaces[id].coords;

Oops, left some editing trash in there, my mail client makes a poor
compiler.

Hi,

Are you trying to use this with the main drawing surface returned by
SDL_SetVideoMode()?

If so, that is why you are seg faulting.

Eddy

Kevron Rees wrote:> int

gfxengine_sdl::drawRect(int
id)
{

if(id==-1)return
-1;

if(surfaces[id].s==NULL)
return
-1;

SDL_BlitSurface(surfaces[id].s,NULL,screen,&surfaces[id].coords);

//seg
faults
here!!!

return
1;
}

Are
you
certain
’&surface[id].coords’
is
resolving
as
you
desire?
???

yes, i thought that might be the issue also so I made coords.w and coords.h match the surface->w and h. What gets me is the only change I have to make is to comment out the SDL_FreeSurface() line and bingo! it works.


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