SDL question
Moderator: Coders of Rage
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
SDL question
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.
- programmerinprogress
- 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
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)
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
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
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: SDL question
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.Ginto8 wrote:When you free an SDL_Surface with SDL_FreeSurface, you can then use the surface again right?
I think.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- programmerinprogress
- 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
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.
this seems to work for me, and I get no memory leaks.
Code: Select all
if(!Surface)
{
// then re-assign the pointer to the new surface
Surface = Load_Image("filenamefornewimage"); )
}
---------------------------------------------------------------------------------------
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
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
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: SDL question
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: SDL question
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
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.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: SDL question
why wouldn't it work?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
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: SDL question
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.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: SDL question
Uhhh... what?MarauderIIC wrote:I just had SDL_Surface* surface; surface = NULL, which wouldn't set the original pointer to null -- just the local one.
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: SDL question
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;
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 realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: SDL question
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
or
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*.
Code: Select all
FreeSurface( SDL_Surface &*surface )
Code: Select all
FreeSurface( SDL_Surface *&surface )
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: SDL question
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.