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 thisRegards.
-
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();
}
}
-
Hi
QNetworkConfigurationManager ncm1;
Is that global variable ?
If yes, it will be created before main and before QApplication. -
@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.
-
@SGaist said in QDBusConnection: systerm Dbus connection created before QCoreApplication:
AFAIK,
QString
,QStringList
andQList
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 ? -
@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