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

Author Topic: [Solved; Explanation missing] 2 RenderWindows in different Threads  (Read 1508 times)

0 Members and 1 Guest are viewing this topic.

Jaberwocky

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hello everyone,

I encounter a weird problem when using two RenderWindows in different threads.
I'm not sure how to explain it correctly, so I will go throw it step by step.

When I boot my application a DLL (Prototari.dll) will be loaded, which is using the SFML 2.5.1 - it creates a window and loads a texture to use - but it does not render yet!

After this a second DLL (SplashScreen.dll) is loaded which will also use the SFML 2.5.1. to display a SplashScreen. While the Prototari-RenderWindow was created by the MainThread, the SplashScreen-RenderWindow will be created in a new Thread. Then, the SplashScreen-RenderWindow will run its event loop and render an image for like 2 seconds. Then the window will be closed, the thread will terminated and the SplashScreen.dll gets unloaded.

NOW the Prototari-RenderWindow starts rendering. After hitting "Enter" a new thread will load Textures, but this will fail with the message "Failed to activate OpenGL context" because the "handle is invalid".

When I dont execute the SplashScreen, everything will work. If I keep the SplashScreen active while the Prototari Window starts rendering, it will crash.



I then created a new DLL which will just open a RenderWindow (instead of using Prototari) and then close it after the SplashScreen is done.
Now I will get this message when the destructor of the RenderWindow was called.

So.... What might be the problem?

Edit: Turns out I mistook the order or the different RenderWindows. The SplashScreen-RenderWindow will be the first one, then the Prototari-RenderWindow is created.


Solution:

So.... I tried something different now, since the order for the 2 RenderWindows was:
1.) Creation in Non-MainThread
2.) Creation in MainThread

I decided to swap the order - now it is working! Yeah!

But I wonder: Why? Is it important that the first RenderWindow was created by the MainThread?
« Last Edit: May 04, 2019, 08:37:05 pm by Jaberwocky »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: [Solved; Explanation missing] 2 RenderWindows in different Threads
« Reply #1 on: May 05, 2019, 06:33:52 pm »
You need to explicitly handle the OpenGL context for each thread, see https://www.sfml-dev.org/tutorials/2.5/window-opengl.php#managing-opengl-contexts

Also don't forget to poll the events in the right thread: https://www.sfml-dev.org/tutorials/2.5/window-window.php#things-to-know-about-windows

And finally, chances are high, that you shouldn't be using threads anyways. OpenGL isn't multi-threaded and you can run multiple windows in one thread as well. Using multiple threads means that there's some overhead, but more importantly, you're adding a lot of complexity and it can introduce a whole new set of potential bugs.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/