SFML community forums

Help => Graphics => Topic started by: MattF on May 16, 2013, 12:21:33 pm

Title: Freeing GPU memory.
Post by: MattF on May 16, 2013, 12:21:33 pm
Hi.

I have a problem with handling the textures. Simply put i have a level class, which has Texture and Sprite members in it. I load textures from files, set sprites, all is well. But now i want to switch to next level when this one is done. So i call delete from outside and create a new level. But the old textures stay in GPU memory.

So my question is, how can i destroy unused textures properly?
Title: Re: Freeing GPU memory.
Post by: Laurent on May 16, 2013, 12:51:35 pm
Quote
But the old textures stay in GPU memory.
You're doing something wrong. A destroyed sf::Texture releases all its memory.
And by the way, how do you know that the textures stay in GPU memory?
Title: Re: Freeing GPU memory.
Post by: MattF on May 16, 2013, 01:09:55 pm
I downloaded gpu-z, which shows how much of gpu memory is in use. I checked memory load when each map is created in separate runtimes and when one is created after deletion of another. whenn i load map2 after map 1 during one runtime i see more memory used.

Anyway i might have screwed something with new/delete routine, i'll admit i don't have much experience with that. I'll paste few snippets regarding loading/reloading a map.

Controler.cpp:
void TestControler::level_selector()
{
switch ( selector )
        {
        case 2:

                std::cout << "loading Mine level..." << std::endl;
                currentLevel = new MineLevel(world, window, rayCallback);

                break;

        case 3:

                std::cout << "loading City level..." << std::endl;
                currentLevel = new CityLevel(world, window, rayCallback);
               
                break;
///etc...
}
}
//and when i want to switch level:

void TestControler::check_for_destructors()
{
        if (switchLevel)
        {
                delete currentLevel;
                currentLevel = new AnotherLevel(world, window, rayCallback);
                switchLevel = false;
        }
}

 

And constructor and destructor for one of the levels.

CaveLevel::CaveLevel(b2World *aWorld, sf::RenderWindow * aWindow,RayCastListener * rayCast) : world(aWorld), window(aWindow)

{
        load_resources();
        set_layers();
        set_chains();
        ls = new ltbl::LightSystem(AABB(Vec2f(0.0f, 0.0f), Vec2f(1680.0f,1050.0f)), window, "data/lightFin.png",   "data/shaders/lightAttenuationShader.frag");
        rayCallback = rayCast;
        hero = new Hero(world, ls,1100, 735, sitting, rayCallback);
        camera = new Camera(hero);
        setup_lights();
        setup_audio();


}

CaveLevel::~CaveLevel()
{
        delete hero;
        delete camera;
        delete ls;

}

 

All the textures are private members of level class, and they are loaded in load_resources() function.
Title: Re: Freeing GPU memory.
Post by: Laurent on May 16, 2013, 01:21:33 pm
Your code looks ok. It's hard to help you more... :-\
Title: Re: Freeing GPU memory.
Post by: MattF on May 16, 2013, 01:34:04 pm
Hmm, maybe gpu-z i showing false readings, i'll try something else. But i am quite sure there is some problem with loading and deleting either way, because of this:
I have a fireplace sound set at some specific location( say, x = 1000). SoundBuffer and Sound are also members of that level class. Strange thing is, that when i load a new map and go to the same location ( x =1000), sound still plays.

I am fairly sure i screwed something up, but i have no idea what. If anyone has any idea where to look, that'd be greatly appreciated, cause tbh i'm not sure which snippets would be relevant, cause the whole new/delete routine seems fairly straightforward.

Edit: Music member actually gets destroyed and stops. Sound does not. It really boggles my mind.
Title: Re: Freeing GPU memory.
Post by: Laurent on May 16, 2013, 01:35:55 pm
You should use valgrind or similar, to track memory issues.
Title: Re: Freeing GPU memory.
Post by: Nexus on May 16, 2013, 01:42:21 pm
cause the whole new/delete routine seems fairly straightforward.
It may look so in simple cases, but it's definitely not.

You should really have a look at the modern C++ idiom RAII (http://en.sfml-dev.org/forums/index.php?topic=9359.0). If you scroll a bit down, you see an example that clearly shows the disadvantages of manual memory management as well as the advantages of RAII.
Title: Re: Freeing GPU memory.
Post by: MattF on May 16, 2013, 02:08:48 pm
Ok guys, fixed it, thanks for the help.

Turns out i made a pretty big mistake of not making a destructor of my base level class virtual. Polymorphism will get ya;)