If a text is drawn to a window in a separate thread, an OpenGL error is reported when the font is destroyed.
The OpenGL error is caused by SFML's Texture.cpp (when sf::Font is destroyed).
Note that, even if the thread is completely finished and destroyed, the font destruction still causes the error.
The error:
(http://i.imgur.com/wU9x55D.png)
The line to which is referred in Texture.cpp:
glCheck(glDeleteTextures(1, &texture));
https://github.com/SFML/SFML/blob/2.4.1/src/SFML/Graphics/Texture.cpp#L104
A complete and minimal example that causes the error:
#include <SFML/Graphics.hpp>
#include <thread>
void threadFunction(const sf::Text& text)
{
sf::RenderWindow window(sf::VideoMode(800, 600), "");
window.draw(text);
}
int main()
{
{
sf::Font font;
font.loadFromFile("resources/fonts/arial.ttf");
sf::Text text("Text", font);
//text.getLocalBounds(); // uncomment to stop error
{
std::thread thread(threadFunction, text);
thread.join();
} // thread completed and destroyed here
} // font destroyed here - OpenGL error
sf::sleep(sf::seconds(1.f));
return EXIT_SUCCESS;
}
Any ideas as to why this happens? Any solutions?
Should the font be so seemingly linked to the window?
The strange thing - as you may have noticed in the code - is that if you call getLocalBounds() on the text object (and subsequently ensureGeometryUpdate), this error no longer occurs. This, could be considered a workaround (or "solution") but I'm still curious as to what causes the problem in the first place.
Note that this exact example was written and tested (and screenshot) using v2.4.1. However, the problems also occur with v2.4.0 and v2.4.2.