Heya!
I am trying to load an sf::Texture in another thread. According to my googling etc, it seems to me like this should be allowed, as seen in both of these threads:
http://en.sfml-dev.org/forums/index.php?topic=10452.0 http://en.sfml-dev.org/forums/index.php?topic=10344.0So my first question is: Is it totally legit to load an sf::Texture in another thread?
Now, after seeing that it might indeed be allowed, I thought my code was the one that was broken, but then I tried the minimal example found in one of those threads, namely this:
#include <SFML/Graphics.hpp>
struct LoadTexture
{
sf::Texture& texture ;
bool & success ;
std::string filename ;
LoadTexture(sf::Texture& t, bool& s, const std::string& fname)
: texture(t), success(s), filename(fname) {}
void operator()()
{ success = texture.loadFromFile(filename) ; }
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "sf::Thread test");
bool success = false ;
sf::Texture green ;
{
sf::Thread thread(LoadTexture(green, success, "GreenTile.png")) ;
thread.launch() ;
}
// success = green.loadFromFile("GreenTile.png") ;
if ( !success )
return 0 ;
while ( window.isOpen() )
{
sf::Event event ;
while ( window.pollEvent(event) )
{
if ( event.type == sf::Event::Closed )
window.close() ;
}
window.clear() ;
window.draw(sf::Sprite(green)) ;
window.display() ;
}
}
This however exhibits the exact same problem.
The problem I get is a segfault. When backtracing it in GDB, I see the following:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffeb013700 (LWP 20915)]
0x00007ffff697e80f in _int_malloc () from /usr/lib/libc.so.6
(gdb) ba
#0 0x00007ffff697e80f in _int_malloc () from /usr/lib/libc.so.6
#1 0x00007ffff69803d4 in malloc () from /usr/lib/libc.so.6
#2 0x00007ffff724b0e8 in operator new (sz=8) at /build/gcc-multilib/src/gcc-5.3.0/libstdc++-v3/libsupc++/new_op.cc:50
#3 0x00007ffff7548ce8 in (anonymous namespace)::getInternalContext() () from /usr/local/lib/libsfml-window.so.2.3
#4 0x00007ffff7548db5 in sf::priv::GlContext::ensureContext() () from /usr/local/lib/libsfml-window.so.2.3
#5 0x00007ffff7549b03 in sf::GlResource::GlResource() () from /usr/local/lib/libsfml-window.so.2.3
#6 0x00007ffff754768e in sf::Context::Context() () from /usr/local/lib/libsfml-window.so.2.3
#7 0x00007ffff7548cf3 in (anonymous namespace)::getInternalContext() () from /usr/local/lib/libsfml-window.so.2.3
#8 0x00007ffff7548db5 in sf::priv::GlContext::ensureContext() () from /usr/local/lib/libsfml-window.so.2.3
#9 0x00007ffff7549b03 in sf::GlResource::GlResource() () from /usr/local/lib/libsfml-window.so.2.3
#10 0x00007ffff754768e in sf::Context::Context() () from /usr/local/lib/libsfml-window.so.2.3
#11 0x00007ffff7548cf3 in (anonymous namespace)::getInternalContext() () from /usr/local/lib/libsfml-window.so.2.3
#12 0x00007ffff7548db5 in sf::priv::GlContext::ensureContext() () from /usr/local/lib/libsfml-window.so.2.3
#13 0x00007ffff7549b03 in sf::GlResource::GlResource() () from /usr/local/lib/libsfml-window.so.2.3
....
This goes on for thousands of stackframes
I am using self-compiled sfml 2.3 and I have tried building my application with both clang and gcc, same result.
Am I missing something obvious on what I need to do to make it possible to load sf::Textures in other threads, or is this not possible at all atm? In the other threads there was talk about a bug related to glFlush() but it seems to have been fixed years ago.
Any ideas appreciated, thanks.