emqx / qmqtt

MQTT client for Qt

Home Page:https://www.emqx.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reconnection collapse!!!

975150313 opened this issue · comments

When I get the "disconnected" signal, The program crashed

When I get the "disconnected" signal, and And when I call "qmqttclient:: connecttohost()",The program crashed,QT can not capture any error information

void NetWork::disconnected()
{
    try
    {
        qDebug()<<"mqtt disconnected ";
        if(nullptr != this->mqtt && false ==isExit )
        {
            qDebug()<<"disconnectFromHost ";
            if (mqtt->state() == QMqttClient::Disconnected)
            {
                qDebug()<<"====QMqttClient::Disconnected===";
                mqtt->connectToHost();
            } else {
                qDebug()<<"====QMqttClient::Connected===";
                mqtt->disconnectFromHost();
            }
        }
    }
    catch (QException& e)
    {
        qDebug()<<e.what();
    }
    catch (QString& e)
    {
        qDebug()<<e;
    }
    catch (const char* e)
    {
        qDebug()<<e;
    }
    catch (...)
    {
        qDebug()<<"...";
    }
}
commented

I could not reproduce this problem with the example application in this project. Connecting to the disconnected signal and calling connectToHost() worked as expected. Like this:
QObject::connect(&publisher, &QMQTT::Client::disconnected, [&] { publisher.connectToHost(); });

So the question is: why is your application different. Can you tell us what OS you are running and which QT version? Also how do you force the disconnect? For example stopping the broker (server) or disconnecting the client machine from the network.

I could not reproduce this problem with the example application in this project. Connecting to the disconnected signal and calling connectToHost() worked as expected. Like this:
QObject::connect(&publisher, &QMQTT::Client::disconnected, [&] { publisher.connectToHost(); });

So the question is: why is your application different. Can you tell us what OS you are running and which QT version? Also how do you force the disconnect? For example stopping the broker (server) or disconnecting the client machine from the network.

I use qt5.10 development, the way to disconnect is to stop the proxy server,I went to the QT community and found an alternative. I used qtmer to send a reconnect signal, connecttohost, to solve this problem. I don't think this is a good solution, because I used an extra timer, but I don't know why

Did you get some error message respectively stack trace [your example contains some try/catch clauses]?
Other possibility: have you tested your C++ code under a linter or memory detector like valgrind? You could have some subtle memory problem...

commented

I agree with @mwallnoefer. Solving this problem requires a stack trace or steps to reproduce.

With regard to your solution: QMQTT already has an auto reconnect feature, so you do not need an extra timer to do this. You need to call setAutoReconnect and setAutoReconnectInterval before connecting to enable this. Also, calling connectToHost immediately after a disconnect is unlikely to do the job: usually it takes a few seconds (or sometimes a lot more) to make it possible to connect again. For example: if the MQTT broker is restarted, it usually takes a few seconds before it is online again. So, it's better to wait a bit (eg. using auto reconnect).

Thank you for your help, I think I already know,You've helped me a lot,I read the content in the source code.It uses qtimer to realize disconnection and reconnection