Jump to content
  • Advertisement
Sign in to follow this  
kev000

SDL freesurface seg fault

This topic is 3733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
it'll draw before I run changeSurface(), it just doesn't draw if I free the old surface.

Share this post


Link to post
Share on other sites
You didn't answer the first question. If you don't initialise them properly, then you'd be freeing random memory, which will crash.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!