When to Free Surfaces
Moderator: Coders of Rage
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: When to Free Surfaces
Ok, I finally found the leak, it was a surface that was a timer I was using for testing, getting a TTF_RenderText_Solid every frame ><
Another thing I'm confused about though... I have all these pointers pointing to the same surface, which I'm assuming are all 4bytes. When I call the loadImage() it allocates memory and gives me a pointer to that location, which I store in these pointers. So then which pointer do I use when I'm ready to call SDL_FreeSurface? Any of them? And at that point, it's essentially up to my program design to make sure all the other pointers are assigned to NULL so I'm not pointing to deallocated memory right?
Sorry if this is a lot. I can memorize code snippets all day, but I wont be able to really program well unless I know the theory behind what I'm coding.
Another thing I'm confused about though... I have all these pointers pointing to the same surface, which I'm assuming are all 4bytes. When I call the loadImage() it allocates memory and gives me a pointer to that location, which I store in these pointers. So then which pointer do I use when I'm ready to call SDL_FreeSurface? Any of them? And at that point, it's essentially up to my program design to make sure all the other pointers are assigned to NULL so I'm not pointing to deallocated memory right?
Sorry if this is a lot. I can memorize code snippets all day, but I wont be able to really program well unless I know the theory behind what I'm coding.
My love is like a Haddoken, it's downright fierce!
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: When to Free Surfaces
Yes; if you free using any of those pointers, then all of them will essentially be pointing to anywhere in memory, which will result in a segfault if you try to use them.
One way to get around this is to use a texture/image factory which assigns a number or a string to each image, and then you call to draw them by passing the number/string. Then the factory checks if that image is loaded, and if not, loads it. Then it gets drawn.
Also means it's easy to implement caching if your game gets big.
One way to get around this is to use a texture/image factory which assigns a number or a string to each image, and then you call to draw them by passing the number/string. Then the factory checks if that image is loaded, and if not, loads it. Then it gets drawn.
Also means it's easy to implement caching if your game gets big.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: When to Free Surfaces
And this is where <map> comes in yes? I haven't used this yet but I read up on it a bit, and this sounds like what you're describing. Although I suppose I could hand code something that does the same thing.RyanPridgeon wrote:Yes; if you free using any of those pointers, then all of them will essentially be pointing to anywhere in memory, which will result in a segfault if you try to use them.
One way to get around this is to use a texture/image factory which assigns a number or a string to each image, and then you call to draw them by passing the number/string. Then the factory checks if that image is loaded, and if not, loads it. Then it gets drawn.
Also means it's easy to implement caching if your game gets big.
My love is like a Haddoken, it's downright fierce!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: When to Free Surfaces
or a set of dynamic arrays with an arrayLocation([name]), if you don't feel like using STL or figuring out how to use maps.Maevik wrote:And this is where <map> comes in yes? I haven't used this yet but I read up on it a bit, and this sounds like what you're describing. Although I suppose I could hand code something that does the same thing.RyanPridgeon wrote:Yes; if you free using any of those pointers, then all of them will essentially be pointing to anywhere in memory, which will result in a segfault if you try to use them.
One way to get around this is to use a texture/image factory which assigns a number or a string to each image, and then you call to draw them by passing the number/string. Then the factory checks if that image is loaded, and if not, loads it. Then it gets drawn.
Also means it's easy to implement caching if your game gets big.
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.
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: When to Free Surfaces
I suppose you could do it in a number of ways, but a C++ map would definately be an option.
If I was doing it I would probably make my own, such as an array/vector of names against an array/vector of images.. and then just assign them places in the array...
Such as
But I'm not very experienced in this. Maybe someone else will have a better solution :P
If I was doing it I would probably make my own, such as an array/vector of names against an array/vector of images.. and then just assign them places in the array...
Such as
Code: Select all
...
SDL_Surface* images[128];
std::string names[128];
...
SDL_Surface* getImage(std::string namepass){
for(int i=0; i<128; i++){
if (namepass == names[i]){
return images[i];
}
}
}
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: When to Free Surfaces
I usually have a vector of SDL_Surface* (images) that stores all the images I've loaded in my Renderer class.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: When to Free Surfaces
When requesting that the Renderer class draws them do you pass the index of the surface in the vector as a param?MarauderIIC wrote:I usually have a vector of SDL_Surface* (images) that stores all the images I've loaded in my Renderer class.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: When to Free Surfaces
Sounds to me that you've seen your first instance of a shared resource. You should read up on retain counting, shared pointers, and/or memory ownership.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: When to Free Surfaces
No, the character has a SDL_Surface* that points to the same thing that a vector element does. Actually I think I use a map<string, SDL_Surface*>. Then when I initialize the player, the player's image is set to map["PLAYER"]. Then when I go to draw, I just draw the object's (polymorphism) image. The renderer deallocates everything in its map in its destructor.
shared_ptr might be useful, although I don't use it.
shared_ptr might be useful, although I don't use it.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: When to Free Surfaces
Wow.. I'm such a dumbass. That's a billion times better than storing an integer and passing it to Renderer every time.MarauderIIC wrote:No, the character has a SDL_Surface* that points to the same thing that a vector element does. Actually I think I use a map<string, SDL_Surface*>.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!