SDL question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

SDL question

Post 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. :(
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: SDL question

Post 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)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL question

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: SDL question

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL question

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL question

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL question

Post 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;
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL question

Post by MarauderIIC »

I just had SDL_Surface* surface; surface = NULL, which wouldn't set the original pointer to null -- just the local one.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL question

Post 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?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL question

Post 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 :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL question

Post 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*.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL question

Post 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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply