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

Author Topic: sf::Thread error  (Read 17901 times)

0 Members and 1 Guest are viewing this topic.

Ragbot

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
sf::Thread error
« on: May 04, 2021, 02:09:32 pm »
I added a new thread to my code and I keep getting this error...

D:\tmp\source\cp\audio_GUI\src\Mp3.cpp:214:12: warning: deleting 'void*' is undefined [-Wdelete-incomplete]
  214 |     delete rawMp3Data;
      |            ^~~~~~~~~~
In file included from D:\tmp\source\cp\audio_GUI\include/SFML/System/Thread.hpp:193,
                 from D:\tmp\source\cp\audio_GUI\include/SFML/System.hpp:43,
                 from D:\tmp\source\cp\audio_GUI\include/SFML/Window.hpp:32,
                 from D:\tmp\source\cp\audio_GUI\include/App/window.hpp:4,
                 from D:\tmp\source\cp\audio_GUI\src\window.cpp:1:
D:\tmp\source\cp\audio_GUI\include/SFML/System/Thread.inl: In instantiation of 'void sf::priv::ThreadFunctor<T>::run() [with T = void (au::Window::*)()]':
D:\tmp\source\cp\audio_GUI\include/SFML/System/Thread.inl:39:18:   required from here
D:\tmp\source\cp\audio_GUI\include/SFML/System/Thread.inl:39:34: error: must use '.*' or '->*' to call pointer-to-member function in '((sf::priv::ThreadFunctor<void (au::Window::*)()>*)this)->sf::priv::ThreadFunctor<void (au::Window::*)()>::m_functor (...)', e.g. '(... ->* ((sf::priv::ThreadFunctor<void (au::Window::*)()>*)this)->sf::priv::ThreadFunctor<void (au::Window::*)()>::m_functor) (...)'
   39 |     virtual void run() {m_functor();}
      |                         ~~~~~~~~~^~

Build finished with error(s).
The terminal process terminated with exit code: -1.
 

This is my code :

void au::Window::loop()
{
    sf::Thread thread_new(&mouse_pos_loop);
    thread_new.launch();

    while(r_window.isOpen())
    {
        render();
        event_handler();
       
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: sf::Thread error
« Reply #1 on: May 04, 2021, 02:58:45 pm »
The error is quite precise.

rawMp3Data is seen as a void pointer, which you can delete, since type and thus memory size information is missing.

Why this is happening with a thread, I don't know, especially since I don't know what mouse_pos_loop does.

Personally recommendation: Don't use threads, unless you're an expert on the topic. There are a lot of complexities and issue that threads can introduce and more often than not, it doesn't actually solve the thing you imagined it would solve.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ragbot

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: sf::Thread error
« Reply #2 on: May 04, 2021, 04:23:53 pm »
rawMp3Data is from a different class and has no relation to this code here but I will fix that,

and mouse_pos_loop() is an empty function I was trying to get a live feed of my mouse position in my so is it possible without sf::Thread? 

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: sf::Thread error
« Reply #3 on: May 04, 2021, 04:38:16 pm »
It seems to dislike the type of mouse_pos_loop.
How exactly is mouse_pos_loop declared? Also what exactly are au and Window? Namespaces or classes?
For example a free function in a namespace is different to a member function in a class when it comes to binding them to things like threads.

Ragbot

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: sf::Thread error
« Reply #4 on: May 05, 2021, 07:50:21 am »

au is namespace which contains the class Window

void au::Window::loop()
{
    sf::Thread thread_new(&mouse_pos_loop);
    thread_new.launch();

    while(r_window.isOpen())
    {
        render();
        event_handler();        
    }
}

// Misc //

void au::Window::mouse_pos_loop()
{ r_mouse_pos = sf::Mouse::getPosition(); }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: sf::Thread error
« Reply #5 on: May 05, 2021, 08:29:00 am »
See the example 2 in the documentation on how to pass a member function of a class.

and mouse_pos_loop() is an empty function I was trying to get a live feed of my mouse position in my so is it possible without sf::Thread? 
You're updating your window at 60+fps anyways, that's more than enough for a "live feed", you don't need to spin on another thread.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ragbot

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: sf::Thread error
« Reply #6 on: May 05, 2021, 03:13:42 pm »
Oh ok thx

Ragbot

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: sf::Thread error
« Reply #7 on: May 24, 2021, 09:43:18 am »
So uh now I have another issue with sf::Thread or rather maybe its just not a feature but

when ever I pass in more than 1 variable it gives me a bunch of errors

Quote
$ g++ main.cpp `pkg-config --libs sfml-all`
main.cpp: In function 'int main()':
main.cpp:14:33: error: no matching function for call to 'sf::Thread::Thread(int (*)(const char*, const char*), const char [4], const char [4])'
   14 |  sf::Thread t(&foo, "str", "lol");
      |                                 ^
In file included from D:/msys64/mingw64/include/SFML/System.hpp:43,
                 from main.cpp:2:
D:/msys64/mingw64/include/SFML/System/Thread.hpp:126:5: note: candidate: 'template<class C> sf::Thread::Thread(void (C::*)(), C*)'
  126 |     Thread(void(C::*function)(), C* object);
      |     ^~~~~~
D:/msys64/mingw64/include/SFML/System/Thread.hpp:126:5: note:   template argument deduction/substitution failed:
main.cpp:14:33: note:   mismatched types 'void (C::*)()' and 'int (*)(const char*, const char*)'
   14 |  sf::Thread t(&foo, "str", "lol");
      |                                 ^
In file included from D:/msys64/mingw64/include/SFML/System.hpp:43,
                 from main.cpp:2:
D:/msys64/mingw64/include/SFML/System/Thread.hpp:103:5: note: candidate: 'template<class F, class A> sf::Thread::Thread(F, A)'
  103 |     Thread(F function, A argument);
      |     ^~~~~~
D:/msys64/mingw64/include/SFML/System/Thread.hpp:103:5: note:   template argument deduction/substitution failed:
main.cpp:14:33: note:   candidate expects 2 arguments, 3 provided
   14 |  sf::Thread t(&foo, "str", "lol");
      |                                 ^
In file included from D:/msys64/mingw64/include/SFML/System.hpp:43,
                 from main.cpp:2:
D:/msys64/mingw64/include/SFML/System/Thread.hpp:75:5: note: candidate: 'template<class F> sf::Thread::Thread(F)'
   75 |     Thread(F function);
      |     ^~~~~~
D:/msys64/mingw64/include/SFML/System/Thread.hpp:75:5: note:   template argument deduction/substitution failed:
main.cpp:14:33: note:   candidate expects 1 argument, 3 provided
   14 |  sf::Thread t(&foo, "str", "lol");
      |                                 ^
In file included from D:/msys64/mingw64/include/SFML/System.hpp:43,
                 from main.cpp:2:
D:/msys64/mingw64/include/SFML/System/Thread.hpp:48:23: note: candidate: 'sf::Thread::Thread(const sf::Thread&)'
   48 | class SFML_SYSTEM_API Thread : NonCopyable
      |                       ^~~~~~
D:/msys64/mingw64/include/SFML/System/Thread.hpp:48:23: note:   candidate expects 1 argument, 3 provided

code:

#include <iostream>
#include <SFML/System.hpp>
#include <string>

int foo(const char x[], const char y[])
{
        while(1)
        std::cout << "Func foo(int) << param: " << x << std::endl;
}

int main()
{

        sf::Thread t(&foo, "str", "lol");
        t.launch();

        while(1)
        {
                std::cout << "main thread" << std::endl;
        }

        return 0;
}
 

So is there any way to use multiple parameters?
or do I have to go with std::thread?

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: sf::Thread error
« Reply #8 on: May 24, 2021, 11:08:36 pm »
Just read the documentation - Threads
In your case:
sf::Thread t(std::bind(&foo, "str", "lol"));