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

Author Topic: Problems Threading A Window  (Read 4253 times)

0 Members and 1 Guest are viewing this topic.

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Problems Threading A Window
« 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problems Threading A Window
« Reply #1 on: December 19, 2012, 10:28:25 pm »
Don't declare your window in global scope.
Laurent Gomila - SFML developer

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Problems Threading A Window
« Reply #2 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problems Threading A Window
« Reply #3 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Problems Threading A Window
« Reply #4 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problems Threading A Window
« Reply #5 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?
Laurent Gomila - SFML developer

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Problems Threading A Window
« Reply #6 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?




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.
« Last Edit: December 20, 2012, 11:53:40 am by 7krs »