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

Author Topic: [solved] Error "Failed to activate the window's context  (Read 12857 times)

0 Members and 1 Guest are viewing this topic.

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« on: October 28, 2009, 09:00:49 pm »
Today I updated sfml/branches/sfml2 from revision 1197 to revision 1250 and now my application outputs "Failed to activate the window's context" to stderr every draw call. It comes from the following function in Window.cpp:

Code: [Select]
bool Window::SetActive(bool active) const
{
    if (myContext)
    {
        if (myContext->SetActive(active))
        {
            return true;
        }
        else
        {
            std::cerr << "Failed to activate the window's context" << std::endl;
            return false;
        }
    }
    else
    {
        return false;
    }
}


Call Stack is:
Code: [Select]
MyApp.exe!sf::Window::SetActive(bool active=false)  Line 330 C++
MyApp.exe!sf::RenderWindow::Activate(bool active=false)  Line 81 C++
MyApp.exe!sf::RenderTarget::Flush()  Line 106 + 0x11 bytes C++
MyApp.exe!sf::RenderWindow::OnDisplay()  Line 119 C++
MyApp.exe!sf::Window::Display()  Line 345 + 0xf bytes C++
MyApp.exe!CScreen::display()  Line 187 C++
MyApp.exe!CGraphicEngine::render(bool & rHalt=false, bool & rError=false)  Line 703 C++
MyApp.exe!CGraphicThread::operator()()  Line 69 C++
MyApp.exe!boost::detail::thread_data<CGraphicThread>::run()  Line 57 C++
MyApp.exe!boost::`anonymous namespace'::thread_start_function(void * param=0x00b65c30)  Line 168 C++
msvcr90d.dll!6815dfd3()
[Frames below may be incorrect and/or missing, no symbols loaded for msvcr90d.dll]
msvcr90d.dll!6815df69()
kernel32.dll!76aceccb()
ntdll.dll!7790d24d()
ntdll.dll!7790d45f()


Both SFML and my project are made with VC++ 2008 Express  and I'm using a statically linked version of SFML. Operating System is Windows Vista Home Premium 64 Bit. Graphics card is an ATI Radeon HD4870 with Catalyst 9.2.

Before I start debugging maybe somebody has a hint?!


UPDATE: solved, see here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Error "Failed to activate the window's context
« Reply #1 on: October 29, 2009, 08:25:29 am »
First, make sure that you did a "rebuild all". Sometimes things get completely messed up after a SVN update.

Then, maybe you can try to extract the smallest piece of code that reproduces this problem.
Laurent Gomila - SFML developer

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« Reply #2 on: October 29, 2009, 08:36:40 pm »
Quote from: "Laurent"
First, make sure that you did a "rebuild all". Sometimes things get completely messed up after a SVN update.

Did that already as I always so that. Debugging rule #1 is "first, rebuild everything" :roll:

Quote from: "Laurent"
Then, maybe you can try to extract the smallest piece of code that reproduces this problem.

No, sorry, the project is pretty large and especially the graphics part is in a bad state at the moment as it will get refactored soon anyway.

Instead I tried several different revisions to see in which of them the error appears:

1197 ok
1222 ok
1232 ok
1235 ok
1239 ok
1242 ok
1244 ok
1245 error
1250 error

Revision 1245 is from 27. October and its message is "Various improvements on OpenGL contexts handling".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Error "Failed to activate the window's context
« Reply #3 on: October 29, 2009, 08:48:11 pm »
Can you put a breakpoint to see why myContext->SetActive(active) fails?
Laurent Gomila - SFML developer

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« Reply #4 on: October 29, 2009, 10:00:52 pm »
The function is called with two different, alternating call stacks:

----- 1 -----

Code: [Select]
MyApp.exe!sf::priv::ContextGL::SetActive(bool active=true)  Line 120 C++
MyApp.exe!sf::Window::SetActive(bool active=true)  Line 304 + 0x10 bytes C++
MyApp.exe!sf::Window::Display()  Line 341 + 0xa bytes C++
MyApp.exe!CScreen::display()  Line 187 C++
MyApp.exe!CGraphicEngine::render(bool & rHalt=false, bool & rError=false)  Line 698 C++
MyApp.exe!CGraphicThread::operator()()  Line 69 C++
MyApp.exe!boost::detail::thread_data<CGraphicThread>::run()  Line 57 C++
MyApp.exe!boost::`anonymous namespace'::thread_start_function(void * param=0x00e65c70)  Line 168 C++


priv::ContextGL::SetActive(bool active=true)

we're here: // Activate the context

MakeCurrent() returns true

threadContext is not NULL, so threadContext = this; is not called

priv::ContextGL::SetActive returning true

then everything is fine in Window::SetActive

----- 2 -----

Code: [Select]
MyApp.exe!sf::priv::ContextGL::SetActive(bool active=false)  Line 119 C++
MyApp.exe!sf::Window::SetActive(bool active=false)  Line 304 + 0x10 bytes C++
MyApp.exe!sf::RenderWindow::Activate(bool active=false)  Line 81 C++
MyApp.exe!sf::RenderTarget::Flush()  Line 106 + 0x11 bytes C++
MyApp.exe!sf::RenderWindow::OnDisplay()  Line 119 C++
MyApp.exe!sf::Window::Display()  Line 326 + 0xf bytes C++
MyApp.exe!CScreen::display()  Line 187 C++
MyApp.exe!CGraphicEngine::render(bool & rHalt=false, bool & rError=false)  Line 698 C++
MyApp.exe!CGraphicThread::operator()()  Line 69 C++
MyApp.exe!boost::detail::thread_data<CGraphicThread>::run()  Line 57 C++
MyApp.exe!boost::`anonymous namespace'::thread_start_function(void * param=0x00e65c70)  Line 168 C++


priv::ContextGL::SetActive(bool active=false)

we're here: // Deactivate the context

condition is false, so return threadContext->SetActive(true); is not called

now we're here: // If we got there then something failed

priv::ContextGL::SetActive returning false

then "Failed to activate the window's context" appears in Window::SetActive

----- x -----

Some remarks:

- CGraphicEngine is a singleton which includes all SFML-related classes.
- the singleton is created in the main thread (of my console application)
- so the SFML::Window constructor is called in the main thread
- afterwards CGraphicThread is started
- that one initializes everything of CGraphicEngine...
- ...including a call to SFML::Window::Create
- and afterwards calls the render function of CGraphicEngine continuously
- meanwhile the main thread waits on join of CGraphicThread

So maybe the problem exists because of the thread mixup.

When I do not start a separate thread but do all the graphic stuff from the main thread the "Failed to activate the window's context" message does not appear.

What are the "best practices" regarding SFML (especially windows and graphics) and multithreading?

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
[solved] Error "Failed to activate the window's context
« Reply #5 on: October 29, 2009, 10:38:40 pm »
I'm getting the same error I believe when my game shuts down. It's a very wierd error. I will send you my project in PM Laurent, it's not too big yet.

My call stack:
Code: [Select]

> sfml-window-d.dll!sf::priv::WindowImplWin32::SetActive(bool Active=true)  Line 298 + 0x1c bytes C++
  sfml-window-d.dll!sf::Context::SetActive(bool Active=true)  Line 64 + 0x18 bytes C++
  sfml-graphics-d.dll!sf::priv::GraphicsContext::GraphicsContext()  Line 69 C++
  sfml-graphics-d.dll!sf::Image::DestroyTexture()  Line 766 + 0x8 bytes C++
  sfml-graphics-d.dll!sf::Image::~Image()  Line 117 C++
  sfml-graphics-d.dll!sf::Font::~Font()  + 0x63 bytes C++
  sfml-graphics-d.dll!`sf::Font::GetDefaultFont'::`2'::`dynamic atexit destructor for 'DefaultFont''()  + 0x28 bytes C++
  sfml-graphics-d.dll!_CRT_INIT(void * hDllHandle=0x10000000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 446 C
  sfml-graphics-d.dll!__DllMainCRTStartup(void * hDllHandle=0x10000000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 557 + 0x11 bytes C
  sfml-graphics-d.dll!_DllMainCRTStartup(void * hDllHandle=0x10000000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 507 + 0x11 bytes C
Eugene Alfonso
GTP | Twitter

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Error "Failed to activate the window's context
« Reply #6 on: October 30, 2009, 08:52:56 am »
Quote
What are the "best practices" regarding SFML (especially windows and graphics) and multithreading?

Whenever you start a new thread that uses the window or graphics module, you must declare a sf::Context for the lifetime of the thread. This will ensure that a valid OpenGL context always exists for your graphics calls, even when no window is used.
Code: [Select]
void threadFunc()
{
    sf::Context context;

    ...
}

Maybe you just forgot this step? ;)

Quote
I'm getting the same error I believe when my game shuts down. It's a very wierd error. I will send you my project in PM Laurent, it's not too big yet.

Ok, I'll take a look at it.
Laurent Gomila - SFML developer

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« Reply #7 on: October 30, 2009, 09:46:47 am »
Quote from: "Laurent"
Whenever you start a new thread that uses the window or graphics module, you must declare a sf::Context for the lifetime of the thread. This will ensure that a valid OpenGL context always exists for your graphics calls, even when no window is used.

When I read this correctly I can indeed have several threads calling SFML (windows and graphics) functions - just that I need to have a sf::Context in each of them. Correct?

Quote from: "Laurent"
Code: [Select]
void threadFunc()
{
    sf::Context context;

    ...
}

Maybe you just forgot this step? ;)

I didn't know that this is necessary - did I miss some documentation about "using SFML in several threads"?

Anyway, I'll try it out next Sunday and report back afterwards.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Error "Failed to activate the window's context
« Reply #8 on: October 30, 2009, 09:51:32 am »
Quote
When I read this correctly I can indeed have several threads calling SFML (windows and graphics) functions - just that I need to have a sf::Context in each of them. Correct?

Absolutely.

Quote
I didn't know that this is necessary - did I miss some documentation about "using SFML in several threads"?

You missed it in the API-reference-that-is-not-generated-yet of SFML 2. You also forgot to read the tutorial about multi-threading and graphics that doesn't exist yet :lol:
Laurent Gomila - SFML developer

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« Reply #9 on: October 30, 2009, 09:53:35 am »
Man, damn, I knew I would get in trouble when I broke my time machine last week...

Actually I did switch to the SFML2 branch because there my old problem seems to be gone and I don't need my custom changes within SFML anymore. In addition the "draw call batching" will probably be of great use for me and my terrain tile rendering. But I haven't really verified that, with 400+ FPS performance isn't really an issue yet...

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
[solved] Error "Failed to activate the window's context
« Reply #10 on: October 30, 2009, 02:36:24 pm »
Since I was able to leave work early today (yay, weekend!) I could already test it and, yes, when I allocate a sf::Context in both the threads which I described above the error message does not appear.

Thanks for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Error "Failed to activate the window's context
« Reply #11 on: October 30, 2009, 02:43:28 pm »
I'm glad you finally solved all your problems.

See you for the next one :)
Laurent Gomila - SFML developer