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

Author Topic: Thread running in a class stops when pressing a key  (Read 5727 times)

0 Members and 1 Guest are viewing this topic.

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Thread running in a class stops when pressing a key
« on: October 24, 2013, 08:33:42 pm »
Hi there, i'm new to SFML and a novice to c++ so i came into a problem today when i try to build my own player class. The bug occurs when i press a key in a running thread, everything runs ok until i press a key any key, The function in the class have a while loop when it checks is a key is pressed and if it is then is displays a message in the console.

Here is the code with the problem if anyone knows what i did wrong can you explain it to me.

The player.h

#include "GlobalVariabiles.h"

class Player
{

public:

        Player(void);
        virtual ~Player(void);

        void setPlayerName(const std::string& name);
        void player_move();
       
        std::string getPlayerName();
        sf::Sprite renderPlayer();

private:       

        void setPlayerTexture(const std::string& path);

        sf::Texture player_texture;
        sf::Sprite player_sprite;
        sf::Thread move_thread;
       
};

 

And the player.cpp


#include "Player.h"

bool playerexist;
std::string Player_Name;

Player::Player(void)
        :move_thread(&Player::player_move, this)
{
        playerexist = true;
        std::cout << "A new Player have bean created !" << std::endl;
        this->move_thread.launch();
}

Player::~Player(void)
{
        playerexist = false;
        this->move_thread.terminate();
        //this->move_thread.~Thread();
}

void Player::player_move()
{

        while (playerexist){
                std::cout << "Runing";
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                std::cout << "Player moved Up" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                std::cout << "Player moved Right" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                std::cout << "Player moved Down" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                std::cout << "Player moved Left" << std::endl;                 
                        }      
        }

}

 

Thanks in advance.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Thread running in a class stops when pressing a key
« Reply #1 on: October 24, 2013, 08:47:26 pm »
There are tons of things wrong here but none that might cause this. Have you checked what playerexist is false after you press a key? Maybe player gets destroyed so it's set to false and all is right? Does it print out 'runing'?
Back to C++ gamedev with SFML in May 2023

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thread running in a class stops when pressing a key
« Reply #2 on: October 24, 2013, 08:58:02 pm »
It prints out running all the time this is why i put cout the to see if it really works but stops when i press a key. Thanks for a quick replay. Can you tell me what i did wrong?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Thread running in a class stops when pressing a key
« Reply #3 on: October 24, 2013, 09:14:22 pm »
Reads to anything that isn't atomic should be protected by mutexes(=sf::Mutex and sf::Lock).
Functions that take no params should use () not (void), that's a C relict where () and (void) mean different (and for () - evil) things.
Destructor shouldn't be virtual unless some functions are, but I assume player is going to be base for something so ok.
There is no reason for that string and bool to be global at all(you probably shouldn't use threads in c++, which is probably most hardcore combination for someone inexperienced in both).

For actual issue - debug it? I don't know if sf::Keyboard::* functions are thread safe or not. :-\ You can try debug, add mutex and then see if replacing sf::Keyboard with something helps.
Back to C++ gamedev with SFML in May 2023

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thread running in a class stops when pressing a key
« Reply #4 on: October 24, 2013, 09:30:24 pm »
I get rid of the void in the constructor and destructor and i try to run it in debug mode but i can't figure out how to get it to work properly i get link errors but i did all the things to configure it even check it twice. I fallow http://sfml-dev.org/tutorials/2.1/start-vc.php tutorial for how to set it up but i can only run in realise and not debug.
The only error message that gives me is this:
Unhandled exception at 0x70441BBE (libsndfile-1.dll) in Nagix V1.1.exe: 0xC0000005: Access violation reading location 0xFEEFE50E.
And i can't figure it out what it means.

EDIT:
I get rid of all the if conditions in the while loop and it is only the while and cout functions and it still crashes don't know why.
« Last Edit: October 24, 2013, 09:41:17 pm by pinkahd »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Thread running in a class stops when pressing a key
« Reply #5 on: October 24, 2013, 09:57:56 pm »
On Windows you have separate set of libraries that end with -d for debugging. It's kind of complicated on Windows. :P
You should try get minimal example up and (not) working as described here: http://en.sfml-dev.org/forums/index.php?topic=5559.0
Back to C++ gamedev with SFML in May 2023

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thread running in a class stops when pressing a key
« Reply #6 on: October 24, 2013, 10:19:16 pm »
Well about the debug thing i tried all the thing i can think but i don't know how to make it work ... i try to put SFML_STATIC with and without release works but debug don't i put all the dependencies -d files but i don't know who to make it work.

Here is the debug output:
1>------ Build started: Project: Nagix V1.1, Configuration: Debug Win32 ------
1>Build started 10/24/2013 11:12:58 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Nagix V1.1.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  SoundEngine.cpp
1>  RenderEngine.cpp
1>  Player.cpp
1>  main.cpp
1>d:\programs\programare\game\nagix v1.1\nagix v1.1\main.cpp(49): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1>          d:\programs\visual studio 2012\vc\include\conio.h(131) : see declaration of 'getch'
1>  Generating Code...
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(Music.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(Music.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(SoundSource.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundSource.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(AudioDevice.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(AudioDevice.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(SoundFile.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundFile.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(ALCheck.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(ALCheck.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>RenderEngine.obj : error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2019: unresolved external symbol "public: static class sf::Time const sf::Time::Zero" (?Zero@Time@sf@@2V12@B) referenced in function "private: bool __thiscall sf::SoundStream::fillAndPushBuffer(unsigned int)" (?fillAndPushBuffer@SoundStream@sf@@AAE_NI@Z)
1>D:\Programs\Programare\Game\Nagix V1.1\Debug\Nagix V1.1.exe : fatal error LNK1120: 2 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.31
 

And i tried to simplified my code but it still don't work i tried to run it off the main.


void x()
{
        while (1==1)
        {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                        std::cout << "Player moved Up" << std::endl;
                }
        }
}

int main()
{
        sf::Thread a(&x);
        a.launch();
        std::cout << "ok" << std::endl;
getch();
return 0;
}

 

It's still crashes when i press a key ... maybe this is not the right way to do it ... i don't know.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Thread running in a class stops when pressing a key
« Reply #7 on: October 24, 2013, 10:26:27 pm »
Quote
i put all the dependencies -d files
Quote
1>sfml-audio-s.lib
There's no -d here. You link the release libraries in debug mode, which leads to such crashes as explained in the tutorial in a big red box.
Laurent Gomila - SFML developer

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thread running in a class stops when pressing a key
« Reply #8 on: October 24, 2013, 10:44:49 pm »
Believe me i have put all the lib files. And this problem is affecting also my friend how is using visual studio 2010 and he configure it himself and we still didn't manage how to configure it properly or anything we both started from an empty project and then we fallow the instructions and the only thing that works is release.
look at the screenshot.
Debug

Release

Sorry if i'm not allowed to post images links from my site.

I fallow the tutorial from the sfml, and another one from youtube and still the same thing ...
I'm running windows 7 ultimate using visual studio 2012 i tried with 2010 still the same thing ...

Edit: If someone have the time to view my project i will upload it and post it... i really don't know what i did wrong.
« Last Edit: October 24, 2013, 10:48:10 pm by pinkahd »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Thread running in a class stops when pressing a key
« Reply #9 on: October 24, 2013, 10:53:20 pm »
audio has -s not -d, others are fine.
As I said try to isolate the problem, you don't need audio(or even graphics) here for just threads and input. Your isolated code from before, runs fine for me(Fedora 19), but whether or not these sf::Keyboard::* functions are thread safe might vary by system. :-\
You really don't need threads or the additional complexity that comes with them at this point.
« Last Edit: October 24, 2013, 10:58:02 pm by FRex »
Back to C++ gamedev with SFML in May 2023

pinkahd

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thread running in a class stops when pressing a key
« Reply #10 on: October 24, 2013, 11:06:29 pm »
Thanks for all the help i fixed it the problem was the getch() function in the main. Now everything work perfect exept for the debug part. Thanks for all the help. And i apologize that you lose time with me for a simple problem it was my fault sorry. Have a nice day.