Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. quit error:qsocketnotifier Socket notifiers cannot be enabled or disabled from anther thread
Forum Updated to NodeBB v4.3 + New Features

quit error:qsocketnotifier Socket notifiers cannot be enabled or disabled from anther thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Landy_94
    wrote on last edited by
    #1

    hey everyone.I created a Qt terminal program to generate a library containing TCP network transceivers, and then referenced the library file in another project.The library works normally in another project, but keeps reporting errors when I exit the program.The following is the picture of the error![alt text](微信截图_20210307111935.png image url). It is strange that when I use the Terminate Program button in Qt Creator, the program exits normally ![alt text](QQ截图20210307170946.png image url). The TCPserver thread is attached to the main thread, and no wild Pointers have been found yet.

    kshegunovK 1 Reply Last reply
    0
    • L Landy_94

      hey everyone.I created a Qt terminal program to generate a library containing TCP network transceivers, and then referenced the library file in another project.The library works normally in another project, but keeps reporting errors when I exit the program.The following is the picture of the error![alt text](微信截图_20210307111935.png image url). It is strange that when I use the Terminate Program button in Qt Creator, the program exits normally ![alt text](QQ截图20210307170946.png image url). The TCPserver thread is attached to the main thread, and no wild Pointers have been found yet.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      This is a rather common error. Please run the program in the debugger again, but before that set the environment variable QT_FATAL_WARNINGS to have a value (of one). When the program trips an assertion, which it will at the point of the first message you see extract a backtrace and let us look. In the mean time you could also provide the code for creation and destruction of your sockets and how you start the thread(s) (and why.

      Read and abide by the Qt Code of Conduct

      L 2 Replies Last reply
      3
      • kshegunovK kshegunov

        This is a rather common error. Please run the program in the debugger again, but before that set the environment variable QT_FATAL_WARNINGS to have a value (of one). When the program trips an assertion, which it will at the point of the first message you see extract a backtrace and let us look. In the mean time you could also provide the code for creation and destruction of your sockets and how you start the thread(s) (and why.

        L Offline
        L Offline
        Landy_94
        wrote on last edited by
        #3

        @kshegunov I added QT_FATAL_WARNINGS in the main thread (The code looks like this: qWarning("QT_FATAL_WARNINGS ",1);) . And runnig in the debugger Here is the error code printed in debugger mode

        Debug Error!

        Program: G:\qtcreater\qtcreater\5.14.2\msvc2017_64\bin\Qt5Cored.dll
        Module: 5.14.2
        File: kernel\qcoreapplication.cpp
        Line: 573

        ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x1e6c8251a30. Receiver '' (of type 'QNativeSocketEngine') was created in thread 0x0x1e6c82291f0", file kernel\qcoreapplication.cpp, line 573

        (Press Retry to debug the application)
        Invalid parameter passed to C runtime function.

        kshegunovK 1 Reply Last reply
        0
        • kshegunovK kshegunov

          This is a rather common error. Please run the program in the debugger again, but before that set the environment variable QT_FATAL_WARNINGS to have a value (of one). When the program trips an assertion, which it will at the point of the first message you see extract a backtrace and let us look. In the mean time you could also provide the code for creation and destruction of your sockets and how you start the thread(s) (and why.

          L Offline
          L Offline
          Landy_94
          wrote on last edited by
          #4

          @kshegunov I want to use TCPSocket to add the function of decoding data and encapsulate it into a module library, which can be called as a third party library for others. Therefore, in this third party library, there is only part of TCP network receiving and decoding data. Here is my TCP network receiving code.
          #include <QTcpServer>
          #include <QTcpSocket>
          #include <QtCore>

          class WIFIDevice : public QTcpServer {
          Q_OBJECT
          public:
          WIFIDevice();
          ~WIFIDevice();

          bool init(decode callback);
          

          public slots:
          void socket_connect();

          void socket_readyread();
          
          void accept_error(QAbstractSocket::SocketError socketerror);
          
          void socket_statechange(QAbstractSocket::SocketState state);
          
          void socket_disconnect();
          

          private:
          decode dataCallback;

          QTcpServer* socket_svr;
          QTcpSocket* socket_por;
          
          std::function<void(void*, int)> call_back;
          

          };

          And this is wifi.cpp

          WIFIDevice::WIFIDevice()
          : dataCallback(NULL)
          {}
          WIFIDevice::~WIFIDevice()
          {
          socket_svr->deleteLater();
          }

          bool WIFIDevice::init(decode dataFn)
          {
          this->dataCallback = dataFn;
          socket_svr= new QTcpServer(this);
          connect(socket_svr, &QTcpServer::newConnection, this, &WIFIDevice::socket_connect);
          connect(socket_svr, &QTcpServer::acceptError, this, &WIFIDevice::accept_error);
          this->socket_svr->listen(QHostAddress::AnyIPv4, 8888)
          }
          When I debug here, I find that this happens whenever I start listening.

          L 1 Reply Last reply
          0
          • L Landy_94

            @kshegunov I want to use TCPSocket to add the function of decoding data and encapsulate it into a module library, which can be called as a third party library for others. Therefore, in this third party library, there is only part of TCP network receiving and decoding data. Here is my TCP network receiving code.
            #include <QTcpServer>
            #include <QTcpSocket>
            #include <QtCore>

            class WIFIDevice : public QTcpServer {
            Q_OBJECT
            public:
            WIFIDevice();
            ~WIFIDevice();

            bool init(decode callback);
            

            public slots:
            void socket_connect();

            void socket_readyread();
            
            void accept_error(QAbstractSocket::SocketError socketerror);
            
            void socket_statechange(QAbstractSocket::SocketState state);
            
            void socket_disconnect();
            

            private:
            decode dataCallback;

            QTcpServer* socket_svr;
            QTcpSocket* socket_por;
            
            std::function<void(void*, int)> call_back;
            

            };

            And this is wifi.cpp

            WIFIDevice::WIFIDevice()
            : dataCallback(NULL)
            {}
            WIFIDevice::~WIFIDevice()
            {
            socket_svr->deleteLater();
            }

            bool WIFIDevice::init(decode dataFn)
            {
            this->dataCallback = dataFn;
            socket_svr= new QTcpServer(this);
            connect(socket_svr, &QTcpServer::newConnection, this, &WIFIDevice::socket_connect);
            connect(socket_svr, &QTcpServer::acceptError, this, &WIFIDevice::accept_error);
            this->socket_svr->listen(QHostAddress::AnyIPv4, 8888)
            }
            When I debug here, I find that this happens whenever I start listening.

            L Offline
            L Offline
            Landy_94
            wrote on last edited by
            #5

            微信截图_20210307214224.png

            L 1 Reply Last reply
            0
            • L Landy_94

              微信截图_20210307214224.png

              L Offline
              L Offline
              Landy_94
              wrote on last edited by
              #6

              @Landy_94 I print out the ID of the thread in each class function (including the destructor) and find that when exiting, the thread in which the destructor is located is not the same as the thread ID of the main function.
              I think that's the cause of the problem.But why does Qt create a new thread when it enters the destructor?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Are you doing any threading in that application ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                L 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Are you doing any threading in that application ?

                  L Offline
                  L Offline
                  Landy_94
                  wrote on last edited by
                  #8

                  @SGaist Yes. Is there something wrong with my approach?

                  1 Reply Last reply
                  0
                  • nageshN Offline
                    nageshN Offline
                    nagesh
                    wrote on last edited by
                    #9

                    @Landy_94 as pointed out here

                    the thread in which the destructor is located is not the same as the thread ID of the main function.
                    

                    looks like resource (sockets might be) being created in one thread and getting destructed in another thread.
                    Check with the ownership of the object, Does in your code thread calls any of the resource destructor, which is not owned by it?

                    1 Reply Last reply
                    0
                    • L Landy_94

                      @kshegunov I added QT_FATAL_WARNINGS in the main thread (The code looks like this: qWarning("QT_FATAL_WARNINGS ",1);) . And runnig in the debugger Here is the error code printed in debugger mode

                      Debug Error!

                      Program: G:\qtcreater\qtcreater\5.14.2\msvc2017_64\bin\Qt5Cored.dll
                      Module: 5.14.2
                      File: kernel\qcoreapplication.cpp
                      Line: 573

                      ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x1e6c8251a30. Receiver '' (of type 'QNativeSocketEngine') was created in thread 0x0x1e6c82291f0", file kernel\qcoreapplication.cpp, line 573

                      (Press Retry to debug the application)
                      Invalid parameter passed to C runtime function.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @Landy_94 said in quit error:qsocketnotifier Socket notifiers cannot be enabled or disabled from anther thread:

                      I added QT_FATAL_WARNINGS in the main thread (The code looks like this: qWarning("QT_FATAL_WARNINGS ",1);) . And runnig in the debugger Here is the error code printed in debugger mode

                      This isn't what I wrote. I said that you should set it as an environment variable. Look in your creator project configuration pane and add the variable and the value to the build/run environment.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3
                      • L Offline
                        L Offline
                        Landy_94
                        wrote on last edited by
                        #11

                        guy,I found that I made a mistake when I created a TCPSERVER and passed this pointer to QTcpServer (which was not needed). This should have caused the newly running socket to exist in the new memory thread, causing an exception on subsequent exits.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved