Search code examples
c++multithreadingqtmemory-managementsignals-slots

How do I manage the deletion of an object that has been passed via a signal slot?


I have just added webcam support to my application using videoInput library and OpenCV.

The video stream is in a QThread (producer) and emits a signal sending out a QImage* for my application to update the graphics item which needs it.

The memory management side of it is that I delete the QImage* in the consumer slot once the slot has finished using it. The issue now is that I would like more than one object to accept that QImage* data.

In this instance do I now have to keep track of all the consumers (with a reference count) of the data and then delete the QImage* (most likely in the producer thread) when all of them are completed?


Solution

  • Using smart pointers should do most of the work for you: QSharedPointer