Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.


  • Qt Champions 2017

    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.



  • @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.



  • @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.



  • 微信截图_20210307214224.png



  • @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?


  • Lifetime Qt Champion

    Hi,

    Are you doing any threading in that application ?



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



  • @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?


  • Qt Champions 2017

    @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.



  • 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.


Log in to reply