Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Application Crash if sending very fast to QApplication::instance()->postEvent

    General and Desktop
    crash postevent multithreads
    3
    13
    6804
    Loading More Posts
    • 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.
    • B
      BluTiGeS last edited by BluTiGeS

      Hello,

      I am using QT in a Multithreaded application. Qt runs in a Sub Thread of my Main Application.

      I am using the following code in a class which runs in another thread to post the Events to the QT Application Main Thread part:

       QWidget *focus =  QApplication::focusWidget(); // get Widget with focus
          if( focus )
          {
         Qt::Key key= Qt::Key_Enter;
              QApplication::instance()->postEvent(focus, new QKeyEvent( QEvent::KeyPress, key, Qt::NoModifier));
              QApplication::instance()->postEvent(focus, new QKeyEvent( QEvent::KeyRelease, key, Qt::NoModifier));
          }
      

      If I now calling this function from the non QT Application part everything seems to work fine until I send many events.

      So for example just sending 10 events at once is no problem, but if I send a lot of events the Application crashes after some seconds.

      If I comment out the postEvent part the App keeps running, so this has something to to with this function.
      Am I using it wrong? Does Qt not queue the events?

      Any advice how to solve the application crash under high load?

      Or am I using it totally wrong? How to call postEvent not from the main thread, to be crash safe?

      Regards

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        postEvent is a static method so basically you're not calling it correctly. In any case, what it is your use case ?

        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 Reply Quote 0
        • B
          BluTiGeS last edited by BluTiGeS

          Hi,

          I want to send events from Thread 1 which is the Main Application to Thread 2 where QT is executed using exec.

          I want to call a function in Thread 1 if this thread receives a XML message over TCP with the action for QT which then sends the regarding Events to thread 2 (QT).

          It works already so Display gets changed and so on but if i send too fast, the Application crashes.

          is there a limit of sending commands to the focused widget?

          Regards

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion last edited by

            Why do you want to send events to focused widget? What if another widget gets focus?
            And why don't you use signals/slots?
            Are you sure you need multi-threading? You can do TCP communication asynchronously in Qt.

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

            1 Reply Last reply Reply Quote 0
            • B
              BluTiGeS last edited by

              Hi,

              the architecture is needed because we are running multiple applications which communicate with each other.
              I also did the same implementation using a signal/slot approach but it also crashes if it gets under high load.

              The background of this is to simulate user interaction by sending a XML command to the Main Application which then calls the QT part.

              If i put a sleep of 1 sec after posting the events it works , so I am wondering why it crashes if it gets fast events.

              It seems to process handling crashes on fast calls (50-100 events a sec).

              Many Thanks

              1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion last edited by

                Where exactly does your application crash?
                Maybe you just have an synchronisation issue in your code since you use multi-threading.

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

                1 Reply Last reply Reply Quote 0
                • B
                  BluTiGeS last edited by

                  Hi,
                  I narrowed it down that it crash during postEvent call.

                  If i comment the postEvent out all works fine. Also If I set a sleep from 500 ms it works.

                  Maybe the other application sends too fast the events that qt does not have time to handle them and then crashes?

                  1 Reply Last reply Reply Quote 0
                  • jsulm
                    jsulm Lifetime Qt Champion last edited by

                    Are you sure it is the postEvent call itself and not the code which is triggered by postEvent?

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

                    1 Reply Last reply Reply Quote 0
                    • B
                      BluTiGeS last edited by

                      I am just sending to the focused widget, already checking if the focus is set

                      if( focus )
                      

                      or do you mean it could occur that the focused widget is not available anymore during the postEvent?

                      how to prevent this?

                      1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion last edited by

                        I mean do you have any code which is executed when you call postEvent? So, do you have any event handlers?
                        Yes it can happen that after if(focus) focus is not valid any more. This is one of the problem you can get in multi-threaded applications.

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

                        1 Reply Last reply Reply Quote 0
                        • B
                          BluTiGeS last edited by

                          Hi,

                          I just let the GUI handle the event .
                          For example if a button is focused and it gets the event for pressing the key the screen will change to a different widget. Or if left right is posted by the events the focus will move in the widget to a different button.

                          But the events are handled in the widgets themselves then, so no bypass done here just normal Qt implementation with Signal and Slots.

                          Regards

                          1 Reply Last reply Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            Since you are running several applications, why not use IPC in your design ? That way you wouldn't need to do hacks to communicate between them

                            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 Reply Quote 0
                            • B
                              BluTiGeS last edited by

                              Many thanks for all your help,

                              I now switched from postEvent to sendEvent, so it is not like fire and forget and I can handle the returns.

                              Also now I can fire a lot of sendEvents and no crash appears anymore. So this solves it for me.

                              Regards

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post