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. Is it safe for a QObject's thread affinity to be a deleted QThread?

Is it safe for a QObject's thread affinity to be a deleted QThread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 415 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.
  • Guy GizmoG Offline
    Guy GizmoG Offline
    Guy Gizmo
    wrote on last edited by
    #1

    Consider the following code:

    QThread *thread = nullptr;
    QObject *object = nullptr;
    
    void setup() {
        QThread *thread = new QThread;
        QObject *object = new QObject;
        object->moveToThread(thread);
        thread->start();
    }
    
    void cleanup() {
        thread->exit(0);
        thread->wait(1000);
        
        if (!thread->isFinished()) {
            // handle error condition
        }
        
        delete thread;
    }
    
    void postEventToThread() {
        if (!object) { return; }
        QEvent *event = new QEvent(QEvent::User);
        qApp->postEvent(object, event);
    }
    

    If postEventToThread() is called after setup() and before cleanup(), then everything should work properly and an event will get posted to object's event queue and processed in thread.

    However, what happens if cleanup() is called before postEventToThread()?

    In that case object still exists and is a valid object, but its thread has been deleted. Does object automatically start behaving as though it has no thread affinity? Or is this an error?

    I tested it out and it doesn't seem to print any warnings, cause any crashes, or otherwise behave badly. But I want to check and make sure that deleting an object's thread (but not the object itself) and posting an event to it is allowed in Qt before I commit to it.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      From the QThread docs, "Deleting a running QThread (i.e. isFinished() returns false) will result in a program crash. Wait for the finished() signal before deleting the QThread." Dependent on your error handling, your code runs this risk if the thread does not stop inside your timeout.

      Also, "Note that deleting a QThread object will not stop the execution of the thread it manages."

      Calling QThread::exit() should stop Qt event processing in that thread. At a quick look, I would not expect Qt posted events to continue being processed after this point, regardless of whether the OS still schedules the thread related to the deleted QThread.

      Guy GizmoG 1 Reply Last reply
      0
      • C ChrisW67

        From the QThread docs, "Deleting a running QThread (i.e. isFinished() returns false) will result in a program crash. Wait for the finished() signal before deleting the QThread." Dependent on your error handling, your code runs this risk if the thread does not stop inside your timeout.

        Also, "Note that deleting a QThread object will not stop the execution of the thread it manages."

        Calling QThread::exit() should stop Qt event processing in that thread. At a quick look, I would not expect Qt posted events to continue being processed after this point, regardless of whether the OS still schedules the thread related to the deleted QThread.

        Guy GizmoG Offline
        Guy GizmoG Offline
        Guy Gizmo
        wrote on last edited by
        #3

        @ChrisW67 said in Is it safe for a QObject's thread affinity to be a deleted QThread?:

        From the QThread docs, "Deleting a running QThread (i.e. isFinished() returns false) will result in a program crash. Wait for the finished() signal before deleting the QThread." Dependent on your error handling, your code runs this risk if the thread does not stop inside your timeout.

        Also, "Note that deleting a QThread object will not stop the execution of the thread it manages."

        Calling QThread::exit() should stop Qt event processing in that thread. At a quick look, I would not expect Qt posted events to continue being processed after this point, regardless of whether the OS still schedules the thread related to the deleted QThread.

        Don't worry, my code makes sure that the thread has stopped before deleting it.

        The question is simply about what happens if an object's thread affinity is set to a deleted QThread. I just wanted to make sure that its thread being a pointer to a deleted object was not going to cause any crashes, particularly if a function running in another thread tries to post an event to it.

        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