Freeing up textures, windows and renderers

Hey guys, still rather new to SDL/SDL2 and I’ve been messing with it for a little and ran into a problem.

I’m trying to create a multi windowed project. For example, I want to build a tool bar, and have an about button that pops open a new window with information inside of it. I created the main window, and the new window, and they work great… until it comes time to close them. When I do free assets using the method of :

Code:

void close_aboutwin()
{
SDL_FreeTexture(abouttex_bg);
abouttex_bg = NULL;

SDL_RenderClear(about_ren);
SDL_DestroyRenderer(about_ren);
about_ren = NULL;

SDL_DestroyWindow(about_win);
about_win = NULL;

}

What happens is that little (if any) of the additional ram utilized to create said window is actually freed, and opening it again afterwards leads to increasing amounts of ram usage (program opens at 13m ram, open about window, program uses 15m of ram, close ram window, program uses 14.5m of ram, open it again, program uses 16.5m of ram, and so on and on until the computer utilizes all of the ram and bsods) similar to a memory leak.

Am I trying to clear my assets incorrectly here or something?

You should first try wrapping any/all of your free/destroy functions in if
statements like so:

if ( abouttex_bg != NULL ) { SDL_FreeTexture( abouttex_bg ); }

You may also want to add some printf/debug output to see which blocks of
memory are actually being cleared.

You could also try commenting out your call to SDL_RenderClear( about_ren
); I think (don’t have the SDL sources handy right now) that
SDL_RenderClear may add about_ren to an internal queue to be cleared, and
the when it executes, about_ren may already be destroyed and then be
invalid. That’s just a guess, though - my first suggestion is probably
going to give you some better information.

-Alex

I don’t recommend that style. SDL_DestroyTexture (I don’t know where
SDL_FreeTexture came from) does a NULL check (!texture), just like standard
memory management functions. Otherwise, we should all have wrappers that
do a NULL check anyway.

Jonny DOn Fri, Sep 6, 2013 at 9:23 AM, Alex Barry <alex.barry at gmail.com> wrote:

You should first try wrapping any/all of your free/destroy functions in if
statements like so:

if ( abouttex_bg != NULL ) { SDL_FreeTexture( abouttex_bg ); }

You may also want to add some printf/debug output to see which blocks of
memory are actually being cleared.

You could also try commenting out your call to SDL_RenderClear( about_ren
); I think (don’t have the SDL sources handy right now) that
SDL_RenderClear may add about_ren to an internal queue to be cleared, and
the when it executes, about_ren may already be destroyed and then be
invalid. That’s just a guess, though - my first suggestion is probably
going to give you some better information.

-Alex


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

My mistake, was working with surfaces at the time of writing… I do use SDL_DestroyTexture.

I don’t particularly know how to check the memory to see what is happening, but I did check that the textures were equal with null after destroying them(which they were)… but the memory issue persists.