SDL_FreeSurface(); questin

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?

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

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:
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:
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.> 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?

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

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

Thank you both.

was not really sure how it worked before.

closed.? !

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.

:slight_smile:

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.

:slight_smile:

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?

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.

:slight_smile: