Sign in to follow this  

SDL freesurface seg fault

Recommended Posts

Hey guys, can't figure out why I seg fault when I try to draw this surface. I'm trying to swap a surface in a list with new surface then free the old one. when I try to draw the surface, it segfaults. here is some code to illustrate: struct surfaceID{ SDL_Surface *s ... }; vector<surfaceID> surfaces; 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); return 1; } int gfxengine_sdl::changeSurface(int &id,SDL_Surface *newsurf) { cout<<"id="<<id<<endl; if(newsurf==NULL)return -1; if(id==-1) { cout<<"gfxengine_sdl::changeSurface(): surface doesn't currently exist. creating..."<<endl; surfaceID s; s.s=newsurf; s.coords.x=screenRect.x; s.coords.y=screenRect.y; s.coords.w=s.s->w; s.coords.h=s.s->h; surfaces.push_back(s); id=surfaces.size()-1; return 1; } if(surfaces[id].s!=NULL) { SDL_FreeSurface(surface[id].s); ///this causes a crash ///SDL_FreeSurface(surface[id].s);///this is okay but memory leak!!! //temp=surfaces[id].s; } surfaces[id].s=newsurf; return 1; }

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this