Accessing tgui::ChatBox::Ptr through sf::Thread

Started by heaven31415, 30 May 2016, 16:34:35

heaven31415

Hello folks. I have a problem with understanding why this code isn't working: https://pastebin.com/BsGwvZab

This code is:
1. Creating chatBox widget.
2. Creating sf::Thread with function receiveMessage which is accessing chatBox every 100 milliseconds.
3. Repeatedly updating / clearing / drawing chatBox widget.

From debugging I know this: Unhandled exception at 0x7761E066 (ntdll.dll) in TGUI Forum Bug Example.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x06C72FFC).

I know also that line 11 (look at pastebin code) is cause of the problem. In this line I'm trying to call tgui::ChatBox::addLine from sf::Thread instance. I don't understand completely why this is happening, I think that probably there is a race condition between two threads which should be avoided.

Information about my hardware and software:
a) OS: Windows 7 Ultimate 64-bit with SP1
b) Compiler: Visual Studio 2015 with Update 2
c) TGUI and SFML: built from source ( SFML commit: e00d160), both linked dynamically.

If you are missing any important information that is necessary to help, just tell me - I will try to provide it as fast as I can.

texus

TGUI isn't thread-safe (and neither is sfml), so accessing a widget from 2 threads at the same time is undefined behavior.

I suggest that you find some concurrent queue class (or use the queue from the std library but with locking yourself) and inside your thread you add the strings to the queue while in the main thread you read from that queue and you add the strings to the chatbox. That way only the main thread uses TGUI and nothing can go wrong.

heaven31415