LaurentGomila/SFML — Issue #120: SFML + OpenGL + threads = leaks
Author: Ceylo
Created: 2011-11-02 20:00:33+00:00
State: open

The automatic OpenGL context handling done by SFML when using several threads seems to be leaking.
Results from SFML sources of the Git repository from November 1st, using Mac OS X 10.6.8 with an Intel GMA X3100 graphics card.

The following code leaks by ~0.6MB/sec, but only if I keep both the sf::Texture and sf::Context objects:

```c++
#include
#include

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;
}
```

Here the memory usage grows up by ~9MB/sec without the sf::Context object, and by ~1MB/sec with the sf::Context object. And the leak is reduced if the image size defined by TEX_SIZE is lowered.

```c++
#include
#include

#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;
}
```

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