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

Author Topic: [Solved] Freeing memory and sf::Texture  (Read 2993 times)

0 Members and 1 Guest are viewing this topic.

upseen

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
[Solved] Freeing memory and sf::Texture
« on: December 07, 2013, 10:22:39 am »
Hello,

Given the minimal code below, at first run, in the Windows' task manager, my application is getting around 7000K memory, and after pressing a key (loading a texture), it takes 9000K. The problem is that memory stays at 9000K forever after, even when i free the texture memory, and reload it. Is this normal?:

#include <SFML/Graphics.hpp>
int main(int argc, char* argv[])
{
    sf::RenderWindow window( { 800, 600 }, "Testwindow" );
    window.setFramerateLimit( 60 );

    sf::Texture* tex{ nullptr };
    sf::Sprite sprite;

    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
                if ( event.type == sf::Event::Closed ) {
                        window.close();
                }
                else if ( event.type == sf::Event::KeyReleased ) {
                        if ( tex ) {
                                delete tex;
                                tex = nullptr;
                                sprite = sf::Sprite();
                        }
                        else {
                                tex = new sf::Texture();
                                tex->loadFromFile( "test.png" );
                                sprite.setTexture( *tex );
                        }
                }
        }

        window.clear();
        if ( tex ) {
                window.draw( sprite );
        }
        window.display();
    }

    if ( tex ) delete tex;
    return 0;
}

I'm using SFML2.1. Thanks.
« Last Edit: December 07, 2013, 11:03:07 am by upseen »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Freeing memory and sf::Texture
« Reply #1 on: December 07, 2013, 10:37:55 am »
First you should not manage memory manually with new/delete. Use smart pointers instead, they make use of RAII.

Secondly the task managed is not the way to monitor memory consumption. Allocating memory via OS is an expensive operation and since one allocation often doesn't comd alone, the OS is "smart" and assignes more memory than requested to your application and "frees" the memory only when either needed by the OS/other applications or when the unused memory is big enough.

In general if you strictly follow RAII you should not have to worry about memory consumption too much if at all. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

upseen

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Freeing memory and sf::Texture
« Reply #2 on: December 07, 2013, 11:02:43 am »
Secondly the task managed is not the way to monitor memory consumption. Allocating memory via OS is an expensive operation and since one allocation often doesn't comd alone, the OS is "smart" and assignes more memory than requested to your application and "frees" the memory only when either needed by the OS/other applications or when the unused memory is big enough.

Explains, thanks.

And actually I'm originally using the std smart pointers and co for the resource manager. Just had to make it as simple as possible. Thanks again ;)

 

anything