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

Author Topic: Multithreading, splash screens and sf::Sprites  (Read 3374 times)

0 Members and 1 Guest are viewing this topic.

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Multithreading, splash screens and sf::Sprites
« on: May 20, 2011, 03:36:40 pm »
I am writing a splash screen class to display while loading. I want it to be created on the main thread and then display itself on a separate thread until I tell it to quit.

This was working fine. However, I also want to display an image in this splash screen window, so I need an sf::Sprite. However, whenever I add the sf::Sprite, the program crashes when it's destructor is called (saying Windows has triggered a breakpoint).

Here is my code; any help fixing this problem would be greatly appreciated :D

Code: [Select]

class SplashScreen {
sf::Image Image;
sf::Font Font;
sf::Thread* Thread;
bool Running;
unsigned Width, Height;
const std::string Caption, Heading;

void Execute()
{
sf::Sprite bg(Image);
bg.Scale((float)Width/(float)Image.GetWidth(), (float)Height/(float)Image.GetHeight());
Running = true;
sf::RenderWindow Window(sf::VideoMode(Width, Height, 32), Caption, sf::Style::None);
while (Running)
{
Window.Draw(bg);
Window.Display();
}
}

public:
SplashScreen(unsigned Width, unsigned Height, const std::string& Heading, const std::string& Caption, const std::string& ImageFilename, const std::string& FontFilename = "arial.ttf")
: Width(Width), Height(Height), Caption(Caption), Heading(Heading)
{
if (!Image.LoadFromFile(ImageFilename))
throw Exceptions::BadFile(ImageFilename);

if (!Font.LoadFromFile(FontFilename))
throw Exceptions::BadFile(FontFilename);

Thread = new (std::nothrow) sf::Thread(&SplashScreen::Execute, this);
if (!Thread)
throw Exceptions::BadAllocation();
}
~SplashScreen()
{
Stop();
delete Thread;
}
void Run()
{
Thread->Launch();
}
void Stop()
{
Running = false;
Thread->Wait();
}
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multithreading, splash screens and sf::Sprites
« Reply #1 on: May 20, 2011, 03:42:48 pm »
Can you run the debugger (F5) and show the call stack when the crash occurs?
Laurent Gomila - SFML developer

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Multithreading, splash screens and sf::Sprites
« Reply #2 on: May 20, 2011, 03:54:33 pm »
Copied and pasted below (sorry it's a bit messy).

[edit]This was from Visual C++; there was a yellow arrow on the top line (I assume this indicates the exception position) and a green arrow on the first line with System.exe (which is my executable file).

Quote

ntdll.dll!77c40474()    
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]   
    ntdll.dll!77c029c0()    
    ntdll.dll!77bbcd77()    
>   System.exe!std::_Tree<std::_Tset_traits<sf::ResourcePtr<sf::Image> *,std::less<sf::ResourcePtr<sf::Image> *>,std::allocator<sf::ResourcePtr<sf::Image> *>,0> >::erase(sf::ResourcePtr<sf::Image> * const & _Keyval)  Line 1398 + 0x57 bytes   C++
    System.exe!sf::Resource<sf::Image>::Disconnect(sf::ResourcePtr<sf::Image> & observer)  Line 78   C++
    System.exe!sf::ResourcePtr<sf::Image>::~ResourcePtr<sf::Image>()  Line 61   C++
    System.exe!sf::Sprite::~Sprite()  + 0x57 bytes   C++
    System.exe!Icanos::System::SplashScreen::Execute()  Line 31 + 0x21 bytes   C++
    System.exe!sf::priv::ThreadMemberFunc<Icanos::System::SplashScreen>::Run()  Line 58 + 0x33 bytes   C++
    sfml-system-d-2.dll!685b07b6()    
    sfml-system-d-2.dll!685b0ef9()    
    sfml-system-d-2.dll!685bad43()    
    sfml-system-d-2.dll!685bace4()    
    kernel32.dll!773433ca()    
    ntdll.dll!77ba9ed2()    
    ntdll.dll!77ba9ea5()

PhiLLe

  • Newbie
  • *
  • Posts: 36
    • View Profile
Multithreading, splash screens and sf::Sprites
« Reply #3 on: May 20, 2011, 04:15:22 pm »
Your code works for me. I am using MinGW and SFML2 :)

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Multithreading, splash screens and sf::Sprites
« Reply #4 on: May 20, 2011, 04:29:54 pm »
Strange. I've just tried building with MinGW, and it does work then. Must be something to do with MSVC++ then :S

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Multithreading, splash screens and sf::Sprites
« Reply #5 on: May 20, 2011, 06:34:48 pm »
Update: I just put exactly the same code in a new solution file in Visual C++ Express, and it worked, so I have no idea what the problem was.

 

anything