Page 2 of 2

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 1:57 pm
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.

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 2:03 pm
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. :)

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 2:12 pm
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.

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 2:47 pm
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. ;)

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 2:49 pm
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

Re: When to Free Surfaces

Posted: Sat Apr 18, 2009 6:35 pm
by MarauderIIC
I usually have a vector of SDL_Surface* (images) that stores all the images I've loaded in my Renderer class.

Re: When to Free Surfaces

Posted: Sun Apr 19, 2009 10:35 am
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?

Re: When to Free Surfaces

Posted: Sun Apr 19, 2009 1:04 pm
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.

Re: When to Free Surfaces

Posted: Sun Apr 19, 2009 4:14 pm
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.

Re: When to Free Surfaces

Posted: Sun Apr 19, 2009 6:17 pm
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: