Page 1 of 1

SDL question

Posted: Sat Jan 17, 2009 2:31 pm
by Ginto8
When you free an SDL_Surface with SDL_FreeSurface, you can then use the surface again right? I know it's a dumb question but its been bugging me for a while now and I'd like to know the answer. :(

Re: SDL question

Posted: Sat Jan 17, 2009 2:52 pm
by programmerinprogress
Indeed you can,it will work just fine :)

Just remember that an SDL_Surface* is just a pointer, so you're free to use it to point to something else, just remember to deallocate the memory your pointing to before you assign it to something else (you don't want memory leaks, and you're deallocating it by using the FreeSurface call)

Re: SDL question

Posted: Sat Jan 17, 2009 3:47 pm
by MarauderIIC
Ginto8 wrote:When you free an SDL_Surface with SDL_FreeSurface, you can then use the surface again right?
Only if you load a new surface into your SDL_Surface. If you use SDL_FreeSurface and then try to draw the surface you just freed, your program'll crash.
I think.

Re: SDL question

Posted: Sat Jan 17, 2009 4:43 pm
by programmerinprogress
I always use FreeSurface then declare the surface as NULL, then just treat it as any other pointer , to stop yourself from assigining the pointer without ensuring you don't get a memory leak you could write your code to reassign the pointer, under the condition that the pointer is NULL.

Code: Select all

if(!Surface)
{
// then re-assign the pointer to the new surface 
Surface = Load_Image("filenamefornewimage");  )
}
this seems to work for me, and I get no memory leaks.

Re: SDL question

Posted: Sat Jan 17, 2009 8:57 pm
by Ginto8
ok thanks... I might as well make my own version of FreeSurface that frees it then sets it to null, so that I don't have any problems.

Re: SDL question

Posted: Sat Jan 17, 2009 10:29 pm
by MarauderIIC
Hopefully you'll be doing a wrapper. SDL_FreeSurfaceHelper(SDL_Surface* surface) { SDL_FreeSurface(surface); surface = NULL; }
edit hm except that wouldn't work. i guess maybe youd have to do a pointer to a pointer instead

Re: SDL question

Posted: Sun Jan 18, 2009 2:32 pm
by Ginto8
MarauderIIC wrote:Hopefully you'll be doing a wrapper. SDL_FreeSurfaceHelper(SDL_Surface* surface) { SDL_FreeSurface(surface); surface = NULL; }
edit hm except that wouldn't work. i guess maybe youd have to do a pointer to a pointer instead
why wouldn't it work?

edit: Here's how you'd do it (I think?):

Code: Select all

FreeSurface( SDL_Surface * &surface )
{
    SDL_FreeSurface( surface );
    surface = NULL;
}

Re: SDL question

Posted: Sun Jan 18, 2009 7:21 pm
by MarauderIIC
I just had SDL_Surface* surface; surface = NULL, which wouldn't set the original pointer to null -- just the local one.

Re: SDL question

Posted: Mon Jan 19, 2009 8:37 am
by Ginto8
MarauderIIC wrote:I just had SDL_Surface* surface; surface = NULL, which wouldn't set the original pointer to null -- just the local one.
:shock: Uhhh... what?

Re: SDL question

Posted: Mon Jan 19, 2009 2:32 pm
by MarauderIIC

Code: Select all

class Whatever {
  SDL_Surface* surface;
};

FreeSurface(SDL_Surface* surface) {
  SDL_FreeSurface(surface);
  surface = NULL;
  cout << "fs: " << surface << endl;
}

Whatever whatever;
whatever.surface = mySurface;
FreeSurface(whatever.surface);
cout << "main: " << whatever.surface << endl;
Would output 0 the first time but not the second, right? I figured the point was to set Whatever's surface to NULL. So FreeSurface would have to be

Code: Select all

FreeSurface(SDL_Sufrace** surface) {
  SDL_FreeSurface(*surface);
  *surface = NULL;
  cout << "fs: " << *surface << endl;
  surface = NULL; //whatever
}

FreeSurface(&whatever.surface);
cout << "main: " << whatever.surface << endl;
I think... It's not something I've done before, but that seems right :)

Re: SDL question

Posted: Mon Jan 19, 2009 3:15 pm
by Ginto8
I have a c++ book (albeit from 1992, though it still works) that says that when you pass a reference to a function, you don't need to use * if you assign it to something. So would it be like

Code: Select all

FreeSurface( SDL_Surface &*surface )
or

Code: Select all

FreeSurface( SDL_Surface *&surface )
The first one is more likely.

Edit: Actually, the second one is, because it's a reference to a variable of the data type SDL_Surface*.

Re: SDL question

Posted: Mon Jan 19, 2009 3:44 pm
by MarauderIIC
Yeah, I was using a pointer to a pointer. But you're right, a reference to a pointer would also work (and be better practice)