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. Help - QTcpSocket call errorString return wrong string in special case.
Forum Updated to NodeBB v4.3 + New Features

Help - QTcpSocket call errorString return wrong string in special case.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.3k 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.
  • L Offline
    L Offline
    Longnm
    wrote on last edited by Longnm
    #1

    I have a issue with QTcpSocket.
    When I connectToHost with argument ("", 0) and looping with interval 500ms , action get errorString() make crash program.

    bool stop = false;
    QTcpSocket* partnerSocket = new QTcpSocket();
    
    class TaskPool : public QRunnable
    {
        void run() override
        {
            while (!stop) {
              partnerSocket -> connectToHost("", 0);
              qDebug() << this << "Socket State" << (int)partnerSocket->state();
              qDebug() << "Socket Error String : " << partnerSocket -> errorString();
              QThread::msleep(500);
            }
        }
    };
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        TaskPool* task = new TaskPool();
        QThreadPool::globalInstance()->start(task);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        return app.exec();
    }
    

    After about 20min, errorString return some wrong string : empty or unicode string \u*** and program crashed.
    errorString() is member method of QIODevice, so i think QIODevice has the issue with memory.

    Solution:

    1. Void this case by using signal of QAbstractSocket to handle error and reconnect
    2. Using waitForConnected for blocking socket when you synchonize progress.
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums
      Is that code enough to reproduce it?

      L 1 Reply Last reply
      0
      • aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        You need a running event loop to use QTcpSocket. With your code you block the event loop. I don't think that will work.

        Also, what is the use of connecting to a host (which one?) every 500 ms. What do you think happens with the old connection in partnerSocket?

        I would not be surprised if this code has memory leaks.

        Qt has to stay free or it will die.

        L 1 Reply Last reply
        5
        • mrjjM mrjj

          Hi and welcome to the forums
          Is that code enough to reproduce it?

          L Offline
          L Offline
          Longnm
          wrote on last edited by
          #4

          @mrjj : oh, because i edited when type on web :D. I updated code block to confirm.
          Thanks.

          1 Reply Last reply
          1
          • aha_1980A aha_1980

            You need a running event loop to use QTcpSocket. With your code you block the event loop. I don't think that will work.

            Also, what is the use of connecting to a host (which one?) every 500 ms. What do you think happens with the old connection in partnerSocket?

            I would not be surprised if this code has memory leaks.

            L Offline
            L Offline
            Longnm
            wrote on last edited by
            #5

            @aha_1980 : i think, too. But i checked source code connectToHost in case return HostNotFound, I can't see something wrong to memory leak.

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

              Hi,

              Since you are interested in possible failures, why not use waitForConnected ?

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

              L 1 Reply Last reply
              2
              • SGaistS SGaist

                Hi,

                Since you are interested in possible failures, why not use waitForConnected ?

                L Offline
                L Offline
                Longnm
                wrote on last edited by
                #7

                @SGaist : I think the problem can make crash in other case , so i need investigate about it.
                I changed source code using signal error() to handle error message and reconnect when state is disconnected. The program is good look.
                But i can't understand about that problem.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  One question is: why are you retrieving the error message right after calling connectToHost since you know that this operation may take time and the function is not blocking ?

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

                  L 1 Reply Last reply
                  3
                  • SGaistS SGaist

                    One question is: why are you retrieving the error message right after calling connectToHost since you know that this operation may take time and the function is not blocking ?

                    L Offline
                    L Offline
                    Longnm
                    wrote on last edited by
                    #9

                    @SGaist : This is bug of my friend. He unknow about not blocking socket. I investigated and fix this bug.
                    I think QIODevice has bug with errorString (QIODevice is valid but errorString wrong). And I can't descript about this crash issue.
                    Maybe I will investigate later.
                    Thank for your help !

                    1 Reply Last reply
                    0

                    • Login

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