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

QDBusConnection: systerm Dbus connection created before QCoreApplication



  • Hi,
    I am creating tcp socket server application using qt creator 5.4 on ubuntu. First I connect ubuntu to wifi manually and then in app, I keep searching if certain network is connected or not. when it is connected, it creates server. This works partially. when I run this app, it gives correct info that is if network is connected or not but when I disconnect or connect network while app is running , app never shows changed state of network and gives warning / error :QDBusConnection: system D-bus connection created before QCoreApplication. Application may misbehave. I have qdbus installed still giving this warning. I never get such warning before I added method for scanning state of network. Please can you help me with this

    Regards.


  • Lifetime Qt Champion

    @Anny

    The error messages says it all: you need to create your QCoreApplication/QApplication before you can use DBus (and most other Qt stuff).

    How does your main.cpp look like?



  • @aha_1980 I have created QCoreApplication
    main.cpp code is as follows

    int main(int argc,char *argv[])
    {
    QCoreApplication a(argc , argv);
    server_connection server;
    server.run();
    return a.exec();
    }


  • Lifetime Qt Champion

    @Anny then we need to see the source of server_connection also ;)



  • This is server_connection code I have used

    #include "server_connection.h"

    #include <QList>

    #include <QtNetwork>

    #include <QByteArray>

    #include <QThread>

    #include <QDebug>

    #include <QTimer>

    #include <QTime>

    #include <QtNetwork/QNetworkConfiguration>

    #include <QtNetwork/QNetworkConfigurationManager>

    #include <QtNetwork/QNetworkSession>

    QNetworkConfigurationManager ncm1;

    QStringList WiFisList;
    
    QList<QNetworkConfiguration> netcfgList;
    

    int connection_status=0;

    QString server_rx_data;

    int found_device =0;

    server_connection::server_connection(QObject *parent) :

    QObject(parent)
    

    {

     qDebug()<<"starting ";
    

    }

    void server_connection::run()

    {

    while(true)
    
    {
    
    
    SearchNetwork();
    if( found_device ==1)
    
    { create_server();
    
    
    
    }
    
    
    QThread::sleep(5);
    
    
    }
    

    }

    void server_connection::newConnection()

    {

    socket =server->nextPendingConnection();
    
    connect(socket, SIGNAL(disconnected()),
    
            this ,SLOT(deleteLater()));
    
    connect(socket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    
    qDebug()<<" connected";
    
    connection_status=1;
    

    }

    void server_connection::SearchNetwork()

    {

    netcfgList.clear();
    
    netcfgList = ncm1.allConfigurations();
    
    WiFisList.clear();
    
    int k=0;
    
    qDebug()<<"net confg";
    
    for (auto &x : netcfgList)
    
        {
    
        qDebug() <<"searching";
    
            if ( x.bearerType() == QNetworkConfiguration::BearerWLAN && x.state() ==QNetworkConfiguration::Active)
    
            {
    
                if(x.name() == "")
    
                {
    
                    WiFisList << "Unknown(Other Network)";
    
                }
    
                else
    
                {
    
                    WiFisList << x.name();
    
                    qDebug()<<"wifi"+WiFisList[k];
    
                    if( (WiFisList[k]=="My_network"))
    
                    {
                         if(found_device==0){
                        found_device=1;
    					}
    					 if(found_device==2){
    					 found_device=2;
    					 }
    
                       // check_network.stop();
    
                        qDebug()<<"connected to network ";
    
                    }
    				else found_device=0;
    
                }
    
                k++;
    
              //qDebug() << x.type();
    
            }
    
        }
    

    }

    }

    void server_connection::create_server()

    {

    server = new QTcpServer(this);
    
    const QHostAddress  &add =QHostAddress::AnyIPv4;
    
    
    
    connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));
    
    if(!server->listen(add,1234))
    
    {
    
        qDebug()<<" server could not start";
    
    }
    
    else {qDebug()<<" server  started";
    
        found_device=2;
    
    }
    
    
    qDebug()<<" port "<<server->serverPort();
    

    }

    void server_connection::readFortune()

    {

    QByteArray block2;
    
    block2.clear();
    
    QDataStream in(&block2, QIODevice::ReadWrite);
    
    in.setVersion(QDataStream::Qt_4_0);
    
    in << (quint16)0;
    
    in.device()->seek(0);
    
    in << (quint16)(block2.size() - sizeof(quint16));
    
    if(socket->waitForReadyRead())
    
    {
    
        char rec = socket->readBufferSize();
    
        qDebug() << rec;
    
    }
    
    block2 = socket->readAll();
    
    
    qDebug() << block2.size();
    
    
    qDebug() <<"starts with" <<block2.at(0);
    
    qDebug() <<"ends with" <<block2.at((block2.size())-1);
    
    QString data="#,7,1,$"; // msg received ack
    
    senddata(data);
    
    server_rx_data = QString::fromUtf8((block2.data()));
    
    qDebug() << server_rx_data;
    
    /* to do  */
    
    if(block2.at(0)=='#' && block2.at((block2.size())-1)=='$')
    
    {
    
        QString data="#,7,1,$"; // msg received ack
    
        senddata(data);
    
    }
    

    }

    void server_connection::senddata(QString data_to_app)

    {

    socket->write(data_to_app.toUtf8());
    
    socket->flush();
    

    }

    void server_connection::deleteLater(){

    qDebug()<<"Disconnected";
    
    found_device=0;
    
    check_network.start(5000);
    
    
    connection_status=0;
    
    database.connection_update();
    
    socket->deleteLater();
    
    SearchNetwork();
    

    }

    void server_connection::retry_server()

    {

    if(server->serverError()!='\b'){
    
        QString error_s =server->errorString() ;
    

    qDebug ()<<"srror connection "<< error_s+" "+server->serverError();

    }
    

    }


  • Lifetime Qt Champion

    Hi

    QNetworkConfigurationManager ncm1;

    Is that global variable ?
    If yes, it will be created before main and before QApplication.


  • Lifetime Qt Champion

    @mrjj is fully right, defining the Non-Plain-Old-Data objects in the CPP file as global variables is dangerous.

    They may as well be used before they are constructed which will crash your program!

    This applies to all the following variables:

    QNetworkConfigurationManager ncm1;
    QStringList WiFisList;
    QList<QNetworkConfiguration> netcfgList;
    QString server_rx_data;

    You should put them as member variables into your class, and while at it, you can put the int variables there also.


  • Lifetime Qt Champion

    @aha_1980

    AFAIK, QString, QStringList and QList are not concerned as they have no dependencies on Qt's internal state. That said, I agree that all of these should be handled by the proper class.



  • @mrjj @aha_1980 @SGaist Thank you, Ill define these as member variables and will get back to you.


  • Lifetime Qt Champion

    @SGaist said in QDBusConnection: systerm Dbus connection created before QCoreApplication:

    @aha_1980

    AFAIK, QString, QStringList and QList are not concerned as they have no dependencies on Qt's internal state.

    they don't contribute to the current problem, but may create other problems later.

    That said, I agree that all of these should be handled by the proper class.

    @Anny the reason is, that objects need to be constructed before usage. as a global variable, the time when their constructor runs is undefined. so it is possible, that you access variables before construction, which can even lead to crashes.



  • Hi @aha_1980 as you said I just declared objects just before usage ,this error is gone . I also tried defining these objects in the header file of class ,there is no error.
    Edit: now there is no such warning/error but this code is not giving me status of wifi connection.I mean it should update status every 5 sec, but it just shows status of wifi in the very first iteration.
    I have a doubt I don't know If I can ask in the same thread, I have defined various variables,strings,structures in global.h file as external and I call them in different files. will this crash my application ?


  • Qt Champions 2019

    @Anny Your

    while(true)
    

    loop is blocking Qt event loop. Remove it and use the asynchronous nature of Qt (you can use QTimer firing in 5 seconds intervals).



  • @jsulm yeah i tried using timer but it not working i dont know why. I m using Timer in other classes , there it works fine. So i switched to thread::sleep



  • @jsulm What I did is, I removed while(true) and sleep command and added timer
    connect(&check_network_timer,QTimer::timeout,this,&server_conneciton::run)
    check_network_timer.start(5000);
    This gives me nothing, app crashes after starting timer


  • Lifetime Qt Champion

    @Anny
    Hi
    Where does it crash ?
    You can use the debugger to see what is going on.
    Using timer normally do not crash so there must be some error in the code.



  • @mrjj I tried debugging it using debugger but it gives error box of segmentation fault saying signal received. what does it mean ?


  • Lifetime Qt Champion

    @Anny
    Hi
    It sounds like its crashing. ( segmentation faults = bad pointers, memory issues etc)
    Place break point at top of function that timer calls etc and see if it stops.



  • @mrjj I added break points it does not stop. But when I run this class as QThread, app didnt crash but stuck after starting timer. I used moveToThread method to run it as thread. I also checked debugger and added some breakpoints, still not stopping and giving same segmentation fault. There is something I am missing out,I dont know what


  • Lifetime Qt Champion

    @Anny

    Hi
    Do you start it with debug and not just normal run ?
    It should stop.



  • @mrjj yeah i did it using debug mode. But I got this working now, there was some QThread related issue. Thank you so much everyone


Log in to reply