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

Author Topic: Memory Management by loadfrommemory  (Read 9576 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #15 on: November 13, 2013, 02:23:24 pm »
Is memory growing every time you press '5', or is it growing constantly?
Laurent Gomila - SFML developer

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Memory Management by loadfrommemory
« Reply #16 on: November 13, 2013, 02:24:29 pm »
everytime i press 5, if i put the stuff in the main function without thread i can press 5 untill my finger breaks and it stays at the same allocated memory.

RenderThread.launch() in on(5) -> i can go to 8 gb ram without any problems
funcclass.gogo()  on (5) (Whats exactly what gets executed in the thread) -> i stay at 32600k
« Last Edit: November 13, 2013, 02:27:20 pm by etixpp »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #17 on: November 13, 2013, 02:34:14 pm »
Whenever you're dealing with graphics in a new thread, SFML needs to create a new OpenGL context. And unfortunately it can't destroy it when the thread terminates, since it doesn't know when it happens. So OpenGL contexts accumulate endlessly every time you create a new thread. This is usually not an issue, since threads are not meant to be created / destroyed so many times; it's always better to reuse a thread that always runs in background.

You can try to instanciate a sf::Context yourself at the beginning of the thread function, but I'm not 100% sure it would solve the problem.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Memory Management by loadfrommemory
« Reply #18 on: November 13, 2013, 03:54:43 pm »
Couldn't that internal context get destroyed with the sf::Window/RenderWindow/RenderTexture/Context? I mean if someone uses stuff without having such an object its user fault and the library could be allowed to not do anything or crash. That would prevent this memory leak and alert the user of his buggy code.
Or you could maybe put some code into sf::Thread that wraps the thread-function and cleans up after it returns, but that would be a gamble and only slightly less messy than always having the leak.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #19 on: November 13, 2013, 04:01:53 pm »
Quote
Couldn't that internal context get destroyed with the sf::Window/RenderWindow/RenderTexture/Context?
What do you mean? There's no relation between the internal context and other contexts.

Quote
Or you could maybe put some code into sf::Thread that wraps the thread-function and cleans up after it returns
That wouldn't work with other ways of launching a thread. And since we now have std::thread, sf::Thread tends to get deprecated.
Laurent Gomila - SFML developer

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Memory Management by loadfrommemory
« Reply #20 on: November 13, 2013, 04:10:25 pm »
So if the thread would have a internal while / game loop with own frameset with sf::Time and never end, it should work, right?
Edit: i tried to do it with sf context (Well i just created such a object at the top of the function, did not seem to work, so i tried some stuff like set active true and so on, did not change something aswell, maybe i did something wrong), does not seem to help.
« Last Edit: November 13, 2013, 04:22:31 pm by etixpp »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #21 on: November 13, 2013, 04:18:20 pm »
Yes, that would solve the memory leak problem.
Laurent Gomila - SFML developer

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Memory Management by loadfrommemory
« Reply #22 on: November 13, 2013, 04:24:50 pm »
Allright, big thanks, just tried it out and noticed it works fine, only problem:
The Cpu goes up to 24 when i let the thread non stop running, i already tried to make him as lightly as possible with sf::clock only doing something if 1 second is done and restart the clock.

I think the Windows Sleep() function should send only the internal thread to sleep and let the main function continue if i call it inside the thread(or am i wrong?) but i would like not to use Windows.h stuff if its not absolutly necesary :/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Memory Management by loadfrommemory
« Reply #23 on: November 13, 2013, 04:29:09 pm »
Make the current thread sleep for a given duration.

If you don't give resources back the thread will try to run as fast as possible.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Memory Management by loadfrommemory
« Reply #24 on: November 13, 2013, 04:55:40 pm »
Quote
Couldn't that internal context get destroyed with the sf::Window/RenderWindow/RenderTexture/Context?
What do you mean? There's no relation between the internal context and other contexts.
I was assuming that internal per-thread context was only there for hiding problems from people using library objects without creating a context first. If its just for needing an unused context to allow SFML to use ShareLists/CopyContext or similar window system calls, I would think a single hidden context per program and not per thread would be enough?

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Memory Management by loadfrommemory
« Reply #25 on: November 13, 2013, 04:56:43 pm »
Make the current thread sleep for a given duration.

If you don't give resources back the thread will try to run as fast as possible.
Ah, forgot there is a sf::sleep function, thanks! I think topic can be closed now^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #26 on: November 13, 2013, 05:02:51 pm »
Quote
I was assuming that internal per-thread context was only there for hiding problems from people using library objects without creating a context first
Yes.

Quote
If its just for needing an unused context to allow SFML to use ShareLists/CopyContext or similar window system calls, I would think a single hidden context per program and not per thread would be enough?
Since I don't know when the thread terminates, I can't unbind the context from it, and thus I can't reactivate it in another thread. So I need a new context in each thread.

I admit that this is really badly designed (but at least from user point of view things are super easy). I'll probably try to clean it up one day.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Memory Management by loadfrommemory
« Reply #27 on: November 13, 2013, 05:15:09 pm »
This is confusing me a bit. I thought the documentation said users have to always create a Context or equivalent object manually first to prevent the library from malfunctioning? Wouldn't it then be enough to use a single mutex-protected hidden context just for that moment when the creation for that user Context happens?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Management by loadfrommemory
« Reply #28 on: November 13, 2013, 06:12:36 pm »
Quote
I thought the documentation said users have to always create a Context or equivalent object manually first to prevent the library from malfunctioning?
Please show me where the documentation says that :)
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Memory Management by loadfrommemory
« Reply #29 on: November 13, 2013, 11:42:49 pm »
Ah sorry, it must have been cause of me reading http://sfml-dev.org/tutorials/2.1/window-opengl.php some days ago and misremembering the text at "OpenGL without a Window" to apply more broadly than there is actually said.