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. Crash of the Server Application
Forum Updated to NodeBB v4.3 + New Features

Crash of the Server Application

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 4.1k Views 1 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #5

    Thank you. I wrote:

    @
    void ServerThread::run()
    {
    socket = new QTcpSocket;
    ...
    }
    @

    But now output is:
    @QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QThread(0x547258), parent's thread is QThread(0x5416a8), current thre
    ad is QThread(0x547258)
    Object::connect: No such slot QThread::sendData() in ../ServerTimer/serverthread
    .cpp:20@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #6

      "You're doing it wrong":http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ . That's what happens if people subclass QThread without knowing about the implications this has.

      The socket variable is a member of your QThread subclass. The instance of your QThread subclass lives in the context of the main thread, so the instance of the member variable socket also lives in the main thread. But everything that happens inside the run method of your QThread subclass happens in the context of the new thread. So when you try to asign to the socket member var from inside the run() function you get the "QObject: Cannot create children for a parent that is in a different thread." output.

      Consider using "the worker object approach":http://qt-project.org/wiki/QThreads_general_usage .

      1 Reply Last reply
      0
      • 8Observer88 Offline
        8Observer88 Offline
        8Observer8
        wrote on last edited by
        #7

        Thank you. I'll read about threads.

        Why when I comment the timer does the program works?

        @
        #include "serverthread.h"

        ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )
        {
        m_descriptor = descriptor;
        }

        void ServerThread::run()
        {
        socket = new QTcpSocket;

        if( !socket->setSocketDescriptor( m_descriptor ) )
        {
            emit error(socket->error());
            return;
        }
        

        // timer = new QTimer(this);
        // connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
        // timer->start(1000);

        qDebug() << m_descriptor << " Client Connected";
        
        exec&#40;&#41;;
        

        }

        void ServerThread::sendData() {
        //socket->write( "time" );
        }
        @

        I want to use a timer in my thread.

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

          What do you mean by "works" ?

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

          1 Reply Last reply
          0
          • 8Observer88 Offline
            8Observer88 Offline
            8Observer8
            wrote on last edited by
            #9

            [quote author="SGaist" date="1378153105"]What do you mean by "works" ?[/quote]

            I don't see the message:
            @
            Listening...
            QObject: Cannot create children for a parent that is in a different thread.
            (Parent is ServerThread(0x3d7258), parent's thread is QThread(0x3d16a8), current
            thread is ServerThread(0x3d7258)
            272 Client Connected
            @

            Maybe is this right:

            @timer = new QTimer(0);@

            1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #10

              No... When I ran my application I received from server the word "time" and the report:

              Listening...
              268 Client Connected
              QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QNativeSocketEngine(0x1e1e430), parent's thread is ServerThread(0x627
              258), current thread is QThread(0x6216a8)
              QSocketNotifier: socket notifiers cannot be disabled from another thread
              ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects ow
              ned by a different thread. Current thread 6216a8. Receiver '' (of type 'QNativeS
              ocketEngine') was created in thread 627258", file kernel/qcoreapplication.cpp, l
              ine 535

              This application has requested the Runtime to terminate it in an unusual way.
              Please contact the application's support team for more information.
              Press <RETURN> to close this window...

              @#include "serverthread.h"

              ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )
              {
              m_descriptor = descriptor;
              }

              void ServerThread::run()
              {
              socket = new QTcpSocket;

              if( !socket->setSocketDescriptor( m_descriptor ) )
              {
                  emit error(socket->error());
                  return;
              }
              
              timer = new QTimer(0);
              connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
              timer->start(1000);
              
              qDebug() << m_descriptor << " Client Connected";
              
              exec&#40;&#41;;
              

              }

              void ServerThread::sendData() {
              socket->write( "time" );
              }
              @

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

                Before going further and since you seem to be starting with network + threads... Are you sure you need threads ? (QTcpServer/Socket are asynchronous so you don't really need them). Did you take a look at the QtNetwork module examples ? (Thinking of the client/fortune example and the threaded version)

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

                1 Reply Last reply
                0
                • 8Observer88 Offline
                  8Observer88 Offline
                  8Observer8
                  wrote on last edited by
                  #12

                  Thank you. I will have a many clients for my server. I'll see network examples from Qt SDK.

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qxoz
                    wrote on last edited by
                    #13

                    Take a look to this tutorial:
                    "Advanced Asynchronous QTcpServer with QThreadPool":http://voidrealms.com/viewtutorial.aspx?id=39

                    1 Reply Last reply
                    0
                    • 8Observer88 Offline
                      8Observer88 Offline
                      8Observer8
                      wrote on last edited by
                      #14

                      [quote author="qxoz" date="1378183845"]Take a look to this tutorial:
                      "Advanced Asynchronous QTcpServer with QThreadPool":http://voidrealms.com/viewtutorial.aspx?id=39
                      [/quote]

                      Thank you very much :)

                      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