SFML community forums

Help => Window => Topic started by: 7krs on December 19, 2012, 09:20:56 pm

Title: Problems Threading A Window
Post by: 7krs on December 19, 2012, 09:20:56 pm
Hi,

the code works, it's just that the return code is weird:

#include <SFML/Graphics.hpp>
sf::RenderWindow wnd;

void sig()
{
    wnd.setActive(true);
    wnd.clear(sf::Color::Green);
    wnd.display();
    wnd.setActive(false);
}

int main()
{
    wnd.create(sf::VideoMode(800, 600, 32), "Title", sf::Style::Default);
    wnd.setActive(false);
    sf::Thread ap(sig);
    ap.launch();
    ap.wait();
    return 0;
}
 

This is a minimal example, and it returns 234, or 0xEA in hex.

#include <SFML/Graphics.hpp>
sf::RenderWindow wnd;

void sig()
{
    wnd.setActive(true);
    wnd.clear(sf::Color::Green);
    wnd.display();
}

int main()
{
    wnd.create(sf::VideoMode(800, 600, 32), "Title", sf::Style::Default);
    wnd.setActive(false);
    sf::Thread ap(sig);
    ap.launch();
    ap.wait();
    return 0;
}
 

Will cause a segmentation fault and return 139 (0x8B).

What am I doing wrong?
Title: Re: Problems Threading A Window
Post by: Laurent on December 19, 2012, 10:28:25 pm
Don't declare your window in global scope.
Title: Re: Problems Threading A Window
Post by: 7krs on December 19, 2012, 10:33:05 pm
Problem is I don't, in practice the window is located in a class, and the thread is in another dedicated to drawing.
Title: Re: Problems Threading A Window
Post by: Nexus on December 19, 2012, 10:34:52 pm
And the instance of that class is not global?

Can you represent your problem in a minimal example like above, but with the window as a function-local variable?
Title: Re: Problems Threading A Window
Post by: 7krs on December 19, 2012, 10:50:37 pm
No, not a single instance is global.


#include <SFML/Graphics.hpp>

class Draw
{
public:
    Draw()
    {
    }
    void start()
    {
        thr = new sf::Thread(&Draw::drawer, this);
        thr->launch();
    }
    void setWindow(sf::RenderWindow *wind)
    {
        wnd = wind;
    }
private:
    void drawer()
    {
        wnd->setActive(true);
        wnd->clear(sf::Color::Green);
        wnd->display();
        wnd->setActive(false);
    }
    sf::Thread *thr;
    sf::RenderWindow *wnd;
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Title", sf::Style::Default);
    Draw object;
    window.setActive(false);
    object.setWindow(&window);
    object.start();
    sf::sleep(sf::seconds(2)); // So the thread can finish in peace.
    return 0;
}

returns 234.
Title: Re: Problems Threading A Window
Post by: Laurent on December 20, 2012, 07:57:55 am
Quote
returns 234
How do you see that? Does it crash? If so, have you tried to run the debugger?
Title: Re: Problems Threading A Window
Post by: 7krs on December 20, 2012, 11:47:50 am
Quote
returns 234
How do you see that? Does it crash? If so, have you tried to run the debugger?


(http://imageupload.org/thumb/thumb_244844.png) (http://imageupload.org/en/file/244844/screenshot.png.html)

It comes up in the little terminal window like that. And no it doesn't crash, it just exits this way.


Yes, I have tried the debugger, but I can't seem to step into return 0; (Where dtors are called). The debugger finishes just fine.