The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_FreeSurface(); questin.
tw3tye


Joined: 22 Jan 2012
Posts: 18
Hello.
Lets get started
We have simple "LoadImge" function and main
my question is : in the LoadImage function, Is the "return ret(which is SDL_Surface*)" from "LoadImage" needed to be freed up or,
in main the "Image1" if we do "SDL_FreeSurface(Image1)" will it free it ??? or will i have data leak?
Code:

SDL_Surface* LoadImage(char* File, SDL_Surface* surf_Screen)
{
    SDL_Surface* img;
    SDL_Surface* ret;
    img = IMG_Load(File);
    if(img = NULL)
        return NULL;

    ret = SDL_DisplayFormat(img);
    SDL_FreeSurface(img);
    return ret;
}

int main(int argc, char* args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* surf_Screen;
    SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
   
    SDL_Surface* Image1;
    Image1 = LoadImage("./Images/Image1.png", surf_Screen);

    SDL_FreeSurface(Image1);

    SDL_Quit();
    return 0;
}

Re: SDL_FreeSurface(); questin.
MBrening


Joined: 09 Nov 2009
Posts: 79
Location: Shawnee, Kansas
It looks like your code is on the right track. You can load a surface using one function (LoadImage), return the pointer to that surface (SDL_Surface *ret), and then free it in a different function, just like you have.
I don't see a leak in the code.

However I do see one thing. I don't know why you are passing the second argument to your LoadImage function since it is not used at all. If you intend to use it later, I STRONGLY recommend you initalize the pointer to NULL:
Code:
SDL_Surface *surf_Screen = NULL;


If you mean it to be the main screen's surface, you can initalize it using your existing code by saying:
Code:
surf_Screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);


Right now you have "dangling pointers." Unless you have some reason not to, it is always best to initialize pointers to NULL. From personal experience, it is too easy to forget to set a pointer to a valid address or to NULL and have random bugs pop up.

Other than that, the code looks great.

tw3tye wrote:
Hello.
Lets get started
We have simple "LoadImge" function and main
my question is : in the LoadImage function, Is the "return ret(which is SDL_Surface*)" from "LoadImage" needed to be freed up or,
in main the "Image1" if we do "SDL_FreeSurface(Image1)" will it free it ??? or will i have data leak?
Code:

SDL_Surface* LoadImage(char* File, SDL_Surface* surf_Screen)
{
    SDL_Surface* img;
    SDL_Surface* ret;
    img = IMG_Load(File);
    if(img = NULL)
        return NULL;

    ret = SDL_DisplayFormat(img);
    SDL_FreeSurface(img);
    return ret;
}

int main(int argc, char* args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* surf_Screen;
    SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
   
    SDL_Surface* Image1;
    Image1 = LoadImage("./Images/Image1.png", surf_Screen);

    SDL_FreeSurface(Image1);

    SDL_Quit();
    return 0;
}

djkarstenv


Joined: 29 Sep 2011
Posts: 34
No, don't free up the ret pointer, or you will lose the actual image you just loaded.

In main() freeing Image1 is perfect, essentially Image1 is pointing to the same image that ret was pointing to earlier, so freeing it in main() is all you need. You should not have any memory leaks by the looks of things. However, what is the purpose of the surf_Screen parameter?

-Karsten
Re: SDL_FreeSurface(); questin.
tw3tye


Joined: 22 Jan 2012
Posts: 18
Thank you both.

was not really sure how it worked before.

closed.? !
djkarstenv


Joined: 29 Sep 2011
Posts: 34
If you're using Windows, and you're ever unsure if your game is leaking memory, just hit CTRL-ALT-DEL and open up the Task Manager. Find your game in the Processes List and watch how it performs while in memory. If you have leaks, you will notice soon enough.

:)
tw3tye


Joined: 22 Jan 2012
Posts: 18
djkarstenv wrote:
If you're using Windows, and you're ever unsure if your game is leaking memory, just hit CTRL-ALT-DEL and open up the Task Manager. Find your game in the Processes List and watch how it performs while in memory. If you have leaks, you will notice soon enough.

:)


What if i leak only one SDL_Surface
i cant notice it really, cause it does "call / leak" only once
if i have leak in loop then i may notice that way.

BTW i want to ask ... the ram storage is cleared when i restart / shutdown computer? right?
or i got to unplug from power like old computers?
djkarstenv


Joined: 29 Sep 2011
Posts: 34
yea also true, you will notice the leak more if its in a loop. You can always test your game by making "test loops" and seeing if the memory is leaking or not. I do that sometimes just to check on some pointers etc.

Your RAM is cleared of all game data the minute you exit the game. You don't even have to reboot. Those days of pulling plugs is gone.

:)