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

Author Topic: [SFML2] sf::Window create close memory issue  (Read 7191 times)

0 Members and 1 Guest are viewing this topic.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
[SFML2] sf::Window create close memory issue
« Reply #15 on: December 19, 2009, 07:28:40 pm »
Quote from: "Zerd"
Alone this code creates a leak:

Code: [Select]
#ifdef _DEBUG
   #define _CRTDBG_MAP_ALLOC
   #include <stdlib.h>
   #include <crtdbg.h>
#endif

#include <SFML/graphics.hpp>


int main() {

   sf::RenderWindow renderWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");

#ifdef _DEBUG
   _CrtDumpMemoryLeaks();
#endif

   return EXIT_SUCCESS;
}
You didn't Close the window.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML2] sf::Window create close memory issue
« Reply #16 on: December 19, 2009, 07:42:28 pm »
It closes in its destructor.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6259
  • Thor Developer
    • View Profile
    • Bromeon
[SFML2] sf::Window create close memory issue
« Reply #17 on: December 19, 2009, 11:26:22 pm »
Microsoft's CRT memory leak detector indeed shows leaks (of course after I put the sf::RenderWindow declaration in a local block; checking something before its destruction is pointless).

I also tested the code with my self-written memory tool. When I called the tool's report functions, three memory leaks were observed (24, 20 and 4 bytes). These are the same ones as the CRT stated. But, according to my trace, that memory is freed – the deallocation took place after my function call, so the memory isn't considered freed at report time. I think, Microsoft's memory dump has got the same problem.

By the way, those alleged leaks came up in the following functions:
  • sf::Resource<T>::Resource() – the std::set initialization
  • sf::ThreadLocal::ThreadLocal(void*) – myImpl = new priv::ThreadLocalImpl;
  • sf::Mutex::Mutex() – myMutexImpl = new priv::MutexImpl;
Obviously, this memory is all correctly deallocated. The above constructors are invoked during program startup, before main(). Therefore, the deallocation is performed at program shutdown, after main().

So: Decide wisely which memory observation tool you want to trust. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

Zerd

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SFML2] sf::Window create close memory issue
« Reply #18 on: December 20, 2009, 03:11:26 pm »
Thanks Nexus,

I tested it for myself as well again usingn new and delete with the renderWindow. Which got me to the same result (3 leaks)

What I don't understand is why the memory is freed after the dump-call and not directly when renderWindow is destroyd?

I assume RenderWindow creates a thread. So it makes sence that memory is allocated until main() finished and the thread is terminated. But wouldn't it make more sence to kill the thread when the RenderWindow is destroyed (for example after using "delete renderWindow;" )? That way the memory dump should be ok.

I'm just curious if that might be a solution?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML2] sf::Window create close memory issue
« Reply #19 on: December 20, 2009, 04:42:32 pm »
Quote
I tested it for myself as well again usingn new and delete with the renderWindow. Which got me to the same result

This doesn't change anything compared to a stack allocation, the object is still created and destroyed.

Quote
What I don't understand is why the memory is freed after the dump-call and not directly when renderWindow is destroyd?

Because it has nothing to do with the RenderWindow. It is related to some global objects that SFML uses. You can test it with an empty main(): the "leaks" will still be there ;)

Quote
I assume RenderWindow creates a thread

Absolutely not!
Laurent Gomila - SFML developer

Zerd

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SFML2] sf::Window create close memory issue
« Reply #20 on: December 20, 2009, 06:06:38 pm »
Ok, thanks for making that clear :)

No further questions!
Thx