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

Author Topic: runtime error with code from graphics tutorial  (Read 1474 times)

0 Members and 1 Guest are viewing this topic.

verbrannt

  • Newbie
  • *
  • Posts: 10
    • View Profile
runtime error with code from graphics tutorial
« on: January 29, 2015, 03:29:02 pm »
code from http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php:

#include <SFML/Graphics.hpp>

void renderingThread(sf::RenderWindow *window)
{
    while (window->isOpen()) {
        window->clear();
        window->display();
    }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL");

    sf::Thread thread(&renderingThread, &window);
    thread.launch();

    while (window.isOpen()) {
        sf::Event event;

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
    }

    return 0;
}

Error when close window: Pure virtual function called! because window is closed (and may be deleted), but pointer *window in second thread are still used

i change code, add mutex, and now it works fine, but i'm not sure is this right way to use mutex here?

// main thread
...
if (event.type == sf::Event::Closed) {
    mutex.lock();
    window.close();
    mutex.unlock();
}
...

// second thread
...
    mutex.lock();
    window->clear();
    window->display();
    mutex.unlock();
...
 
« Last Edit: January 29, 2015, 03:37:30 pm by verbrannt »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: runtime error with code from graphics tutorial
« Reply #1 on: January 29, 2015, 03:35:31 pm »
No because your window->isOpen() is not protected. Beside, this might considerably decrease your performance if you have to sync each thread on each frame.

Instead, do something like Pablo did there: http://en.sfml-dev.org/forums/index.php?topic=17368.msg124938#msg124938 (disregard the issue presented in this thread if you're not on Mac).
SFML / OS X developer

verbrannt

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: runtime error with code from graphics tutorial
« Reply #2 on: January 29, 2015, 03:41:18 pm »
thank you

global atomic variable and thread.wait() works well
« Last Edit: January 29, 2015, 03:49:56 pm by verbrannt »