Crash with QtConcurrent + QlocalServer / Qlocalsocket / Qfuture



  • Hello I m using Qt 5.8 on windows and I have a problem with the debug...

    EDIT : My collab on MacOS do not have theses problems !

    In the headers I have :

    #ifndef CONSTANTES_H
    #define CONSTANTES_H
    
    // #define MPV_SERVER_NAME "/tmp/mpv-socket"
    #define MPV_SERVER_NAME "\\\\.\\pipe\\mpv-pipe"
    #define AUTOMATE_SERVER_NAME "test" // "/tmp/client-socket"
    
    const char kVitesseLecture[]="vitesse";
    const char kParamSwitch[]="switch";
    
    const char kJsonSignal[]="signal";
    const char kJsonParams[]="params";
    
    enum signalType {
        kSignalPlay,
        kSignalPause,
        kSignalEnd
    };
    
    // Phase de lecture de liste
    enum phaseliste{
        kPhaseLecture,
        kPhasePause,
        kPhaseEnd
    };
    
    // Vitesses de lecture
    enum speed {
      kVitesseNormale=0,
      kVitesseDouble=1
    };
    
    const char * const audio[]= {
        "Audio/Miley_Cyrus-Jolene.mp3",
        "Audio/Moana - I Am Moana.mp3"
    };
    
    
    #endif // CONSTANTES_H
    

    And

    
    #ifndef SERVEUR_H
    #define SERVEUR_H
    
    #include <QObject>
    #include <QLocalServer>
    #include <QLocalSocket>
    #include <QtConcurrent/QtConcurrent>
    #include <QVector>
    #include "automate.h"
    
    class Serveur: public QObject
    {
        Q_OBJECT
    public:
        explicit Serveur(QObject *parent = 0);
        ~Serveur();
        bool m_running;
        QFuture<void> m_serverLoopThread;
        void clientMessageLoop();
    
    private:
        QLocalServer *m_server;
        QLocalSocket * m_client;
        QLocalSocket *mpv=NULL;
        void sendRequestToMPV();
    
    private slots:
        void connectionFromClient();
        void clientDisconnected();
    
    public slots:
        // Messages reçus de l'automate
        void message(signalType,QVariantMap);
    
    signals:
        //transmettre des commandes vers l'automate
        void signalFromServer(signalType, QVariantMap);
    
    };
    
    #endif // SERVEUR_H
    
    
    #include "serveur.h"
    
    #include <QDebug>
    
    Serveur::Serveur(QObject *parent) :
        QObject(parent),
        m_server(new QLocalServer(this)),
        mpv(new QLocalSocket(this))
    {
        qRegisterMetaType<signalType>("signalType");
    
        mpv->connectToServer(MPV_SERVER_NAME,QIODevice::ReadWrite);
        if (mpv->waitForConnected())
            qDebug() << "connected to mpv";
        else {
            throw mpv->errorString();
        }
    
        // lancer le serveur pour parler avec les clients
        QString serverName(AUTOMATE_SERVER_NAME);
        QLocalServer::removeServer(serverName);
        if (!m_server->listen(serverName)) {
            throw m_server->errorString();
        }
    
        connect(m_server, SIGNAL(newConnection()), this, SLOT(connectionFromClient()));
    
        m_running=true;
        m_serverLoopThread = QtConcurrent::run(this,&Serveur::clientMessageLoop);
    
        sendRequestToMPV();
        qDebug() << "y" ;
    }
    
    Serveur::~Serveur() {
        qDebug() << "del server" ;
        mpv->disconnectFromServer();
        if(m_client)m_client->abort();
        m_server->close();
        m_running = false;
        m_serverLoopThread.waitForFinished();
    }
    
    // slot appelé lorsqu'un client se connecte
    void Serveur::connectionFromClient() {
    
        if (m_client) return; // Un seul client pour l'instant...
        m_client= m_server->nextPendingConnection();
        connect(m_client, SIGNAL(disconnected()), m_client, SLOT(deleteLater()));
        connect(m_client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
        qDebug() << "connexion client" ;
    }
    
    // slot appelé lorsqu'un client se déconnecte
    void Serveur::clientDisconnected() {
        qDebug() << "déconnexion client" ;
        m_client=NULL;
    }
    
    
    // lire les messages des clients
    // on reçoit un truc du genre { "signal" = numero_signal ,
    //    "params" = [<un variant map avec des parametres>] }
    //jsonObject["signal"]=sig;
    //jsonObject[kJsonParams]=QJsonObject::fromVariantMap(params);
    void Serveur::clientMessageLoop() {
        while (m_running) {
            QDataStream in(m_client);
            if ( in.atEnd() ) { // Rien dans la file d'attente
                QThread::msleep(100); // On attend 1/10s et on continue
                continue;
            }
            qDebug() << "msg reçu";
            QString str=QString(in.device()->readLine());
            QByteArray a=str.toUtf8();
            QJsonParseError error;
            QJsonDocument jDoc=QJsonDocument::fromJson(a, &error);
            QJsonObject jsonObject=jDoc.object();
    
            qDebug() << jsonObject[kJsonSignal].toInt();
    
            emit signalFromServer((signalType)jsonObject[kJsonSignal].toInt(),
            jsonObject[kJsonParams].toObject().toVariantMap());
        }
    }
    
    void Serveur::sendRequestToMPV(){
        QJsonObject jsonObject ;
    
    QJsonArray a ;
    a.append("loadfile");
    a.append(audio[0]);
    
        jsonObject["command"]=a;
        QByteArray bytes = QJsonDocument(jsonObject).toJson(QJsonDocument::Compact)+"\n";
        if (mpv!=NULL) {
          mpv->write(bytes.data(), bytes.length());
          mpv->flush();
        }
    
        QDataStream in(mpv);
        if (mpv->waitForReadyRead()) {
            QString str=QString(in.device()->readLine());
            QByteArray an=str.toUtf8();
            QJsonParseError error;
            QJsonDocument jDoc=QJsonDocument::fromJson(an, &error);
            QJsonObject jsonObject2=jDoc.object();
            QVariantMap json_map = jsonObject2.toVariantMap();
            qDebug() << json_map["error"].toString() ;
        } else {
            qDebug() << "ffffff"  ;
        }
    }
    

    I'm running mpv in the windows terminal with

    mpv --idle --input-ipc-server=\\.\pipe\mpv-pipe
    

    I have managed to connect my server to the mpv socket. But not to create a socket for clients (they will have to connect to the server wich can after send requests to mpv). That is why I need 2 sockets.

    When I run the debug...

    Démarrage de C:\Users\simon\Documents\git_ihm3\build-ihm2_serveur-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\ihm2_serveur.exe...
    connected to mpv
    "success"
    y
    Le programme s'est terminé subitement.
    C:\Users\simon\Documents\git_ihm3\build-ihm2_serveur-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\ihm2_serveur.exe crashed.
    

    I think there is a problem of socket with

    #define MPV_SERVER_NAME "\\\\.\\pipe\\mpv-pipe"
    #define AUTOMATE_SERVER_NAME "test" // "/tmp/client-socket"
    

    Or with this just bellow because when I put it in commentary the program doesn't crash :

    m_serverLoopThread = QtConcurrent::run(this,&Serveur::clientMessageLoop);
    

    I will appreciate your help ! Thanks by advance for your help, I don't really know why is this crashing...


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Cab you post the backtrace ?



  • Hey ! Thnak you :)

    Is it this called "backtrace" ?

    0_1492809204501_backtrace.PNG


  • Lifetime Qt Champion

    The backtrace is what you get for the debugger when your application crashes. i.e. the backward list of function that stopped at the crash point.



  • Mmmh this ?

    Thread 5 (Thread 6456.0x4f8):
    #0  0x6ba4cfec in QDataStream::atEnd (this=0x19edfddc) at io\qdatastream.cpp:424
    No locals.
    #1  0x004020bd in Serveur::clientMessageLoop (this=0x84fdd4) at ..\Serveur\serveur.cpp:79
            str = {static null = {<No data fields>}, d = 0x0}
            a = {d = 0x3}
            jDoc = {static BinaryFormatTag = 1936351857, d = 0x3}
            in = {d = {d = 0x0}, dev = 0xa15994f <libstdc++-6!_Znaj+15>, owndev = false, noswap = false, byteorder = QDataStream::BigEndian, ver = 17, q_status = QDataStream::Ok}
            error = {offset = 0, error = QJsonParseError::NoError}
            jsonObject = {d = 0x19b260c0, o = 0x2}
            __PRETTY_FUNCTION__ = "void Serveur::clientMessageLoop()"
    #2  0x00405ebd in QtConcurrent::VoidStoredMemberFunctionPointerCall0<void, Serveur>::runFunctor (this=0x19b25e88) at D:/Qt/5.8/mingw53_32/include/QtConcurrent/qtconcurrentstoredfunctioncall.h:205
    No locals.
    #3  0x00405be0 in QtConcurrent::RunFunctionTask<void>::run (this=0x19b25e88) at D:/Qt/5.8/mingw53_32/include/QtConcurrent/qtconcurrentrunbase.h:136
    No locals.
    #4  0x6b99945b in QThreadPoolThread::run (this=0x19b26130) at thread\qthreadpool.cpp:99
            autoDelete = true
            r = 0x19b25e90
            expired = 25
            locker = {val = 431120576}
            __PRETTY_FUNCTION__ = "virtual void QThreadPoolThread::run()"
    #5  0x6b99b817 in QThreadPrivate::start(void*)@4 (arg=0x19b26130) at thread\qthread_win.cpp:378
            thr = 0x19b26130
            data = 0x19b261e8
    #6  0x73f062c4 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\System32\kernel32.dll
    No symbol table info available.
    #7  0x77050fd9 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #8  0x77050fa4 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #9  0x00000000 in ?? ()
    No symbol table info available.
    
    Thread 4 (Thread 6456.0x187c):
    #0  0x770601cc in ntdll!ZwWaitForWorkViaWorkerFactory () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #1  0x77026a61 in ntdll!TpReleaseTimer () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #2  0x73f062c4 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\System32\kernel32.dll
    No symbol table info available.
    #3  0x77050fd9 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #4  0x77050fa4 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #5  0x00000000 in ?? ()
    No symbol table info available.
    
    Thread 3 (Thread 6456.0xe5c):
    #0  0x770601cc in ntdll!ZwWaitForWorkViaWorkerFactory () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #1  0x77026a61 in ntdll!TpReleaseTimer () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #2  0x73f062c4 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\System32\kernel32.dll
    No symbol table info available.
    #3  0x77050fd9 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #4  0x77050fa4 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #5  0x00000000 in ?? ()
    No symbol table info available.
    
    Thread 2 (Thread 6456.0x10ac):
    #0  0x770601cc in ntdll!ZwWaitForWorkViaWorkerFactory () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #1  0x77026a61 in ntdll!TpReleaseTimer () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #2  0x73f062c4 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\System32\kernel32.dll
    No symbol table info available.
    #3  0x77050fd9 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #4  0x77050fa4 in ntdll!RtlSubscribeWnfStateChangeNotification () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #5  0x00000000 in ?? ()
    No symbol table info available.
    
    Thread 1 (Thread 6456.0x2118):
    #0  0x6baab272 in QWindowsPipeReader::checkPipeState (this=0x19b235d8) at io\qwindowspipereader.cpp:274
            bytes = 431121396
    #1  0x6baab034 in QWindowsPipeReader::startAsyncRead (this=0x19b235d8) at io\qwindowspipereader.cpp:218
            minReadBufferSize = 4096
            bytesToRead = 431109640
            ptr = 0x16650000 "kÿ\220í¥ï"
    #2  0x6baaafcf in QWindowsPipeReader::notified (this=0x19b235d8, errorCode=0, numberOfBytesRead=32) at io\qwindowspipereader.cpp:204
    No locals.
    #3  0x6baab22c in QWindowsPipeReader::readFileCompleted(unsigned long, unsigned long, _OVERLAPPED*)@12 (errorCode=0, numberOfBytesTransfered=32, overlappedBase=0x19b235e4) at io\qwindowspipereader.cpp:263
            overlapped = 0x19b235e4
    #4  0x73c13dc1 in WaitNamedPipeW () from C:\WINDOWS\System32\KernelBase.dll
    No symbol table info available.
    #5  0x770607ed in ntdll!KiUserApcDispatcher () from C:\WINDOWS\SYSTEM32\ntdll.dll
    No symbol table info available.
    #6  0x6baab206 in QWindowsPipeReader::startAsyncRead (this=0x84f7a8) at io\qwindowspipereader.cpp:253
            minReadBufferSize = 431139920
            bytesToRead = 2731492096
            ptr = 0x1 <error: Cannot access memory at address 0x1>
    #7  0x19b235e4 in ?? ()
    No symbol table info available.
    #8  0x6baab960 in QWindowsPipeWriter::waitForNotification (this=0x19b23e08, timeout=0) at io\qwindowspipewriter.cpp:171
            t = {t1 = 39578185782, t2 = 0}
            msecs = 0
    #9  0x6baab6fe in QWindowsPipeWriter::waitForWrite (this=0x19b23e08, msecs=0) at io\qwindowspipewriter.cpp:87
    No locals.
    #10 0x67f1a60e in QLocalSocket::flush (this=0x19b23fb8) at socket\qlocalsocket_win.cpp:313
            d = 0x19b23510
            written = false
    #11 0x00402694 in Serveur::sendRequestToMPV (this=0x84fdd4) at ..\Serveur\serveur.cpp:108
            jsonObject = {d = 0x19b23e88, o = 0x19b26710}
            a = {d = 0x19b23eb0, a = 0x19b26420}
            bytes = {d = 0x19b26858}
            in = {d = {d = 0x19b25fa8}, dev = 0x84fc68, owndev = 156, noswap = 60, byteorder = (QDataStream::LittleEndian | unknown: 1949726660), ver = 375717888, q_status = 8715416}
            __PRETTY_FUNCTION__ = "void Serveur::sendRequestToMPV()"
    #12 0x00401bf3 in Serveur::Serveur (this=0x84fdd4, parent=0x0) at ..\Serveur\serveur.cpp:40
            serverName = {static null = {<No data fields>}, d = 0x19b23ab8}
            __PRETTY_FUNCTION__ = "Serveur::Serveur(QObject*)"
    #13 0x00401670 in qMain (argc=1, argv=0x16657d10) at ..\Serveur\main.cpp:18
            s = {<QObject> = {_vptr.QObject = 0x40a448 <vtable for Serveur+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bd958a0 <qt_meta_stringdata_QObject>, data = 0x6bd95980 <qt_meta_data_QObject>, static_metacall = 0x6bb08bf2 <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x19b23db0}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6be54660 <qt_meta_stringdata_Qt>, data = 0x6be5d2a0 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0x6bd95aa8 <QObject::staticMetaObject>, stringdata = 0x409b60 <qt_meta_stringdata_Serveur>, data = 0x409c40 <qt_meta_data_Serveur>, static_metacall = 0x403b20 <Serveur::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, m_running = true, m_serverLoopThread = {d = {_vptr.QFutureInterfaceBase = 0x6be94c68 <vtable for QFutureInterfaceBase+8>, d = 0x19b25fa8}}, m_server = 0x19b23ee0, m_client = 0xa15994f <libstdc++-6!_Znaj+15>, mpv = 0x19b23fb8}
            m = {<QObject> = {_vptr.QObject = 0x8, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bd958a0 <qt_meta_stringdata_QObject>, data = 0x6bd95980 <qt_meta_data_QObject>, static_metacall = 0x6bb08bf2 <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x1}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6be54660 <qt_meta_stringdata_Qt>, data = 0x6be5d2a0 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0x6bd95aa8 <QObject::staticMetaObject>, stringdata = 0x409dc0 <qt_meta_stringdata_Automate>, data = 0x409ec0 <qt_meta_data_Automate>, static_metacall = 0x403e20 <Automate::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, TpsLecture = 0x84fe48, Lecteur = 0x73b88b67 <LocalFree+39>, statePlay = 0x8b0000, statePause = 0x0, stateFin = 0x73b88b80 <LocalFree+64>}
            r = 1941690656
            a = {<QGuiApplication> = {<QCoreApplication> = {<QObject> = {_vptr.QObject = 0x155ac88 <vtable for QApplication+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bd958a0 <qt_meta_stringdata_QObject>, data = 0x6bd95980 <qt_meta_data_QObject>, static_metacall = 0x6bb08bf2 <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x16657db8}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6be54660 <qt_meta_stringdata_Qt>, data = 0x6be5d2a0 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0x6bd95aa8 <QObject::staticMetaObject>, stringdata = 0x6be6b6a0 <qt_meta_stringdata_QCoreApplication>, data = 0x6be6b860 <qt_meta_data_QCoreApplication>, static_metacall = 0x6bb67718 <QCoreApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, static self = 0x84fe14}, static staticMetaObject = {d = {superdata = 0x6be6b9bc <QCoreApplication::staticMetaObject>, stringdata = 0xa6df0c0 <qt_meta_stringdata_QGuiApplication>, data = 0xa6df540 <qt_meta_data_QGuiApplication>, static_metacall = 0xa2cba42 <QGuiApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0xa6d48e4 <QGuiApplication::staticMetaObject>, stringdata = 0x14867e0 <qt_meta_stringdata_QApplication>, data = 0x1486a60 <qt_meta_data_QApplication>, static_metacall = 0x10016da <QApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}
    #14 0x00405410 in WinMain@16 () at qtmain_win.cpp:111
            argc = 1
            argvW = 0x8b5560
            argv = 0x16657d10
            exitCode = 120
    #15 0x0040750d in main ()
    No symbol table info available.
    

  • Lifetime Qt Champion

    Yes

    You don't check in clientMessageLoop that m_client is valid value.



  • @SGaist Alright thaaaaannk youuuu ! I init it to NULL in the header and it works !

    But how did you see it in the backtrace ?
    There is only "m_client = 0xa15994f <libstdc++-6!_Znaj+15>"
    What does that means ?


  • Lifetime Qt Champion

    I saw in the backtrace where it happened in your code then I check what your code was doing.


Log in to reply