SFML community forums

Help => Window => Topic started by: dejavu on February 21, 2016, 11:35:30 am

Title: [sf::Context] recursive loop, causing crash (loading resources in thread)
Post by: dejavu on February 21, 2016, 11:35:30 am
Callstack:
(...)
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
        appSnakeClient-vc11.exe!sf::Context::Context() Line 61  C++
        appSnakeClient-vc11.exe!`anonymous namespace'::getInternalContext() Line 155    C++
        appSnakeClient-vc11.exe!sf::priv::GlContext::ensureContext() Line 213   C++
        appSnakeClient-vc11.exe!sf::GlResource::GlResource() Line 61    C++
 

Some parts of SFML code:
Context::Context() // <== GOES HERE! constructor is calling inherited constructor GlResource::GlResource
{
    m_context = priv::GlContext::create();
    setActive(true);
}

GlResource::GlResource()
{
    {
        // Protect from concurrent access
        Lock lock(mutex);

        // If this is the very first resource, trigger the global context initialization
        if (count == 0)
            priv::GlContext::globalInit();

        // Increment the resources counter
        count++;
    }

    // Now make sure that there is an active OpenGL context in the current thread
    priv::GlContext::ensureContext();  // <== GOES HERE!
}

void GlContext::ensureContext()
{
    // If there's no active context on the current thread, activate an internal one
    if (!currentContext)
        getInternalContext()->setActive(true); // <== GOES HERE!
}


namespace
{
    sf::Context* getInternalContext()
    {
        if (!hasInternalContext())
        {
            internalContext = new sf::Context; // <== GOES HERE!
            sf::Lock lock(internalContextsMutex);
            internalContexts.insert(internalContext);
        }

        return internalContext;
    }
}

 
Title: Re: [sf::Context] recursive loop, causing crash (loading resources in thread)
Post by: dejavu on February 21, 2016, 12:00:42 pm
I've fixed this issue. Check if it's correct and add it to master branch please.
Title: [sf::Context] recursive loop, causing crash (loading resources in thread)
Post by: eXpl0it3r on February 21, 2016, 01:33:12 pm
It would be interesting to get a minimal and complete example first.
Just because you think you know what's broken, doesn't mean it really is, might also be your code. ;)
Title: Re: [sf::Context] recursive loop, causing crash (loading resources in thread)
Post by: AlejandroCoria on February 21, 2016, 03:58:37 pm
I had the same problem, create a context in the main thread and the other in another thread causes the recursive loop (no matter in what order were created).

I solved using the branch https://github.com/SFML/SFML/tree/feature/no_internal_context (https://github.com/SFML/SFML/tree/feature/no_internal_context).
Title: Re: [sf::Context] recursive loop, causing crash (loading resources in thread)
Post by: binary1248 on February 21, 2016, 05:26:38 pm
This has been known for a while: #989 (https://github.com/SFML/SFML/issues/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 (https://github.com/SFML/SFML/pull/1002) and #989 (https://github.com/SFML/SFML/issues/989).