Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Freeing GPU memory.  (Read 2530 times)

0 Members and 1 Guest are viewing this topic.

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Freeing GPU memory.
« 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Freeing GPU memory.
« Reply #1 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?
Laurent Gomila - SFML developer

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Freeing GPU memory.
« Reply #2 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.
« Last Edit: May 16, 2013, 01:15:44 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Freeing GPU memory.
« Reply #3 on: May 16, 2013, 01:21:33 pm »
Your code looks ok. It's hard to help you more... :-\
Laurent Gomila - SFML developer

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Freeing GPU memory.
« Reply #4 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.
« Last Edit: May 16, 2013, 01:37:07 pm by MattF »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Freeing GPU memory.
« Reply #5 on: May 16, 2013, 01:35:55 pm »
You should use valgrind or similar, to track memory issues.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Freeing GPU memory.
« Reply #6 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. 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Freeing GPU memory.
« Reply #7 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;)