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

Author Topic: Error when loading sf::Texture in another thread  (Read 4206 times)

0 Members and 1 Guest are viewing this topic.

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Error when loading sf::Texture in another thread
« on: February 21, 2016, 04:23:05 pm »
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.0

So 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Error when loading sf::Texture in another thread
« Reply #1 on: February 21, 2016, 05:24:05 pm »
Since OpenGL is not multi-threaded it's recommended to only load the sf::Image from disk in a separate thread and do the loadFromImage on a texture in the main thread.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Error when loading sf::Texture in another thread
« Reply #2 on: February 21, 2016, 05:26:10 pm »
This has been known for a while: #989

Just use https://github.com/SFML/SFML/tree/feature/no_internal_context for now.

The "fix" (and improvement) was submitted quite a while ago, however it has been stuck in "testing hell" since then. The more support the pull request gets, the faster it will eventually get merged. So feel free to leave feedback in #1002 and #989.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Re: Error when loading sf::Texture in another thread
« Reply #3 on: February 21, 2016, 05:39:10 pm »
Thanks for the replies.

This has been known for a while: #989

Just use https://github.com/SFML/SFML/tree/feature/no_internal_context for now.

The "fix" (and improvement) was submitted quite a while ago, however it has been stuck in "testing hell" since then. The more support the pull request gets, the faster it will eventually get merged. So feel free to leave feedback in #1002 and #989.

I built said feature branch and it now works perfectly. I will leave a post in the related issue. Hope it gets merged soon.

 

anything