Menu

Deleting an event reference with Q_NEW_REF from another non-QP thread

Alex Wu
2024-06-24
2024-06-27
  • Alex Wu

    Alex Wu - 2024-06-24

    Hey all,

    I'm working with the POSIX port of qpcpp 7.3.4.

    Since the Qt port is no longer supported, I've been looking for alternative ways to inject events into the Qt event queue from QP.

    My current method is to simply create a reference to an event from a QP AO with Q_NEW_REF, which I then emit in a Qt signal for Qt's event loop to handle. When the Qt side finishes handling the event, it calls Q_DELETE_REF.

    Is this valid use of event references? I see that Q_NEW_REF and Q_DELETE ref are thread safe.

    Thanks in advance.

     
    • Quantum Leaps

      Quantum Leaps - 2024-06-26

      This seems like a very error-prone approach.
      --MMS

       
  • Alex Wu

    Alex Wu - 2024-06-26

    May be a stupid question, but how so? Would the risk just be in how the Qt thread may forget to delete the reference?

     
    • Quantum Leaps

      Quantum Leaps - 2024-06-26

      Yes, Qt thread can easily forget to delete the reference. The whole approach also pretty much defeats the central idea of automatic event recycling in QP.

      But a more basic question is why you need to post QP events to Qt event queues? I mean, once you are on the Qt side you use Qt events, not QP events...

       
      • Alex Wu

        Alex Wu - 2024-06-27

        I was looking for a way to generically adapt QP events into Qt. I am technically still posting Qt events that just contain a pointer to a QP event so there's no need for an explicit conversion.

        I could just copy the QP event and post that to the Qt event queue instead, which is less risky. Just wondering if doing it copyless like this is fine if I'm sure the reference will be freed.

         
  • Quantum Leaps

    Quantum Leaps - 2024-06-27

    Copying QP events into Qt event queues sounds like the safest option.

    Passing pointers to QP events inside some other data structures is probably incorrect because it leads to sharing the event instances. The problem is that the QP Framework does not "know" about such sharing, so it might recycle an event prematurely (so your pointer to event will be "dangling"). I hope that this makes sense to you.

    --MMS

     
    👍
    1

    Last edit: Quantum Leaps 2024-06-27

Log in to post a comment.