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

Author Topic: Memory management of OpenGL objects with multithreading  (Read 2988 times)

0 Members and 1 Guest are viewing this topic.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Memory management of OpenGL objects with multithreading
« on: November 01, 2011, 06:27:04 pm »
Hello,

While working on sfeMovie I noticed a memory leak related to the OpenGL contexts.

I have 2 background threads where I do graphical work through the SFML Graphics package and I kill these threads when stopping the movie playback, and relaunch them when playing again.

I noticed a memory increase by around 10MB each time I restart the movie playback (stop [kill thread] + play [relaunch thread]). I can't remember how I thought of this but... if I manually declare a sf::Context object in each of the two thread functions, the memory leak is reduced by 90%. Thus I suppose that when exiting the background thread, the associated OpenGL context isn't properly released, whereas with the manual OpenGL context handling it's ok (I still need to figure out what's the remaining 10% leaking).

Isn't something going wrong in the SFML automatic OpenGL context handling?

Ceylo

PS: going to write a minimal code ASAP.
Want to play movies in your SFML application? Check out sfeMovie!

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Memory management of OpenGL objects with multithreading
« Reply #1 on: November 01, 2011, 06:52:14 pm »
With this code, memory usage grows up by 0.6MB every second, and manually creating a sf::Context object or changing the texture size doesn't change anything:

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

void thread_func(void *data)
{
// Reuse the same texture object
sf::Texture *tex = (sf::Texture *)data;

// Should just erase the previous data
tex->Create(16, 16);
}

int main()
{
sf::Texture tex;
sf::Thread th(thread_func, (void *)&tex);

// Create one thread/sec
while (true)
{
th.Launch();
th.Wait();

sf::Sleep(1000);
}

return 0;
}
Want to play movies in your SFML application? Check out sfeMovie!

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Memory management of OpenGL objects with multithreading
« Reply #2 on: November 01, 2011, 06:57:18 pm »
With this code however, the memory usage grows up by ~9MB/sec without the sf::Context object, and by ~1MB/sec with the sf::Context object.

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

#define TEX_SIZE 2048

sf::Image sharedImage;

void thread_func(void *data)
{
// Reuse the same texture object
sf::Texture *tex = (sf::Texture *)data;

sf::Context ctx;

// Should just erase the previous data
tex->LoadFromImage(sharedImage);
}

int main()
{
sf::Texture tex;
sharedImage.Create(TEX_SIZE, TEX_SIZE, sf::Color::Red);
sf::Thread th(thread_func, (void *)&tex);

// Create one thread/sec
while (true)
{
th.Launch();
th.Wait();

sf::Sleep(1000);
}

return 0;
}


Edit: and the memory leak is reduced if I use a smaller image.
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Memory management of OpenGL objects with multithreading
« Reply #3 on: November 01, 2011, 07:35:28 pm »
You should add an issue in the task tracker, otherwise I may forget it :P
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Memory management of OpenGL objects with multithreading
« Reply #4 on: November 01, 2011, 08:59:12 pm »
Note that the following code leaks too (by ~0.6MB/sec), but only if I keep both the sf::Texture and sf::Context objects:
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

void thread_func()
{
sf::Context ctx;
}

int main()
{
sf::Texture tex;
sf::Thread th(thread_func);

// Create one thread/sec
while (true)
{
th.Launch();
th.Wait();

sf::Sleep(1000);
}

return 0;
}


I've also tested empty thread function, loading/creating image/texture in the main thread without getting any leak.

I'll add a issue in the task tracker soon.
Want to play movies in your SFML application? Check out sfeMovie!

 

anything