Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDBusConnection: systerm Dbus connection created before QCoreApplication
Forum Updated to NodeBB v4.3 + New Features

QDBusConnection: systerm Dbus connection created before QCoreApplication

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 5 Posters 4.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anny
    wrote on last edited by
    #3

    @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();
    }

    aha_1980A 1 Reply Last reply
    0
    • A Anny

      @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();
      }

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anny
        wrote on last edited by Anny
        #5

        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();

        }
        

        }

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Hi

          QNetworkConfigurationManager ncm1;

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

          aha_1980A 1 Reply Last reply
          2
          • mrjjM mrjj

            Hi

            QNetworkConfigurationManager ncm1;

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

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #7

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

            Qt has to stay free or it will die.

            SGaistS 1 Reply Last reply
            0
            • aha_1980A aha_1980

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

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              aha_1980A 1 Reply Last reply
              0
              • A Offline
                A Offline
                Anny
                wrote on last edited by
                #9

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

                1 Reply Last reply
                0
                • SGaistS SGaist

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

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

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

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Anny
                    wrote on last edited by Anny
                    #11

                    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 ?

                    jsulmJ 1 Reply Last reply
                    0
                    • A Anny

                      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 ?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • A Offline
                        A Offline
                        Anny
                        wrote on last edited by
                        #13

                        @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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Anny
                          wrote on last edited by
                          #14

                          @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

                          mrjjM 1 Reply Last reply
                          0
                          • A Anny

                            @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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

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

                            1 Reply Last reply
                            1
                            • A Offline
                              A Offline
                              Anny
                              wrote on last edited by
                              #16

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

                              mrjjM 1 Reply Last reply
                              0
                              • A Anny

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

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #17

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

                                1 Reply Last reply
                                1
                                • A Offline
                                  A Offline
                                  Anny
                                  wrote on last edited by Anny
                                  #18

                                  @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

                                  mrjjM 1 Reply Last reply
                                  0
                                  • A Anny

                                    @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

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    @Anny

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

                                    1 Reply Last reply
                                    2
                                    • A Offline
                                      A Offline
                                      Anny
                                      wrote on last edited by Anny
                                      #20

                                      @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

                                      1 Reply Last reply
                                      2

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved