Jump to content
  • Advertisement
Sign in to follow this  

[SFML][C++] Freeing RenderWindow pointer memory

This topic is 2853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm kinda new to C++ and SFML and I was just woundering if their is a special way to "delete" SFML objects.
Or is it okey to use the normal delete keyword ?


sf::RenderWindow *renderwindow = new sf::RenderWindow();
delete renderwindow;

Share this post

Link to post
Share on other sites
Most of SFML, including sf::RenderWindow, is RAII. This means whenever it goes out of scope, it cleans up itself.


void myFunc() //myFunc() begins a 'scope', and that scope ends when the function ends.
sf::RenderWindow window1(...);

if(....) //An if() statement begins another scope, which ends when the if() statement ends.
sf::RenderWindow window2(...);
} //'window2' goes out of scope, and is cleaned up automaticly.

} //'window1' goes out of scope, and is cleaned up automaticly.

If you use 'new', you better have a good reason for doing so. wink.png
'new' basically tells C++, "Forget your stupid scope cleanup! I'm going to handle it myself!"
Anything you new, you have to delete.

Since sf::RenderWindow, being a well-designed C++ class, handles it's own new-ing, and upon destruction, handles it's own delete-ing, you don't have to worry about it, as long as you actually understand scope and class constructors/destructors.

If you manually 'new' sf::RenderWindow, you are saying:
You: "[color=#008080]Compiler! Make me a new sf::RenderWindow!"

  • RenderWindow: "[color=#8b4513]Compiler! Make me a new [...all the data that RenderWindow needs...]"
    (and later:)
    You: "[color=#008080]Compiler! Delete me a "sf::RenderWindow!"
    RenderWindow: "[color=#8b4513]Compiler! Delete [...all the data that RenderWindow uses internally...]"

    sf::RenderWindow is already handling all it's own data internally. There's no need for you to manually new or delete anything, unless you want to. And there's no legitimate reason why you'd want to, unless you need to for your code to work properly. And with sf::RenderWindow, if your new to C++ and SFML, you probably don't need it to.

    If you are new to C++, you might want to avoid 'new' and 'delete' for a few weeks for practice, and try to think of ways to get your code to work without it. You'll probably find that 99% of the time, you don't need to manage the memory itself. smile.png
    It's important to understand scope, though.

    Here's another example of scope:

    int test1;

    test1 = 27; //Perfectly fine, because 'test1' is still within scope, even though it's being accessed by a deeper scope.
    int test2;
    } //'test2' goes out of scope.

    test1 = 5; //Perfectly fine, still within scope.
    test2 = 5; //Compiler error! No such thing as 'test'.

    I can't find offhand a good tutorial teaching scope or I'd link you to it, but it's something important you need to understand. Once you understand it, then you need to understand that new() and delete() ignores scope, and lets you take control of the lifespan of the variable manually. You don't want to manually do anything that you don't have to.

    Have fun with SFML, by the way, it's a very good API.

  • [size=2]And make it proper, with breadcrumbs!

    Share this post

    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

    Sign me up!