When to Free Surfaces

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

User avatar
Maevik
Chaos Rift Junior
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

Post by Maevik »

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.
My love is like a Haddoken, it's downright fierce!
User avatar
RyanPridgeon
Chaos Rift Maniac
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

Post by RyanPridgeon »

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. :)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Maevik
Chaos Rift Junior
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

Post by Maevik »

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. :)
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.
My love is like a Haddoken, it's downright fierce!
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: When to Free Surfaces

Post by Ginto8 »

Maevik wrote:
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. :)
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.
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. ;)
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
RyanPridgeon
Chaos Rift Maniac
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

Post by RyanPridgeon »

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

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];
        }
    }
}


But I'm not very experienced in this. Maybe someone else will have a better solution :P
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: When to Free Surfaces

Post by MarauderIIC »

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.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

MarauderIIC wrote:I usually have a vector of SDL_Surface* (images) that stores all the images I've loaded in my Renderer class.
When requesting that the Renderer class draws them do you pass the index of the surface in the vector as a param?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Johannes
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Sun Apr 19, 2009 3:30 am

Re: When to Free Surfaces

Post by Johannes »

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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: When to Free Surfaces

Post by MarauderIIC »

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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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*>.
Wow.. I'm such a dumbass. That's a billion times better than storing an integer and passing it to Renderer every time. :lol:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply