views:

258

answers:

1

I have a QThread that contains a QUDPsocket (socket is member not local to QThread::run(), maybe I should change that from what I am reading). This QThread is instantiated in my QMainWindow class ie the GUI thread(I am not calling move to thread). Is it still safe to use waitForReadyRead or do I absolutly need to instantiate the QThread in main.cpp or call moveToThread() for it to be thread safe. I am getting intermittent double free exception inside the call to waitForReadyRead in the present way of doing it(sometimes I dont get it for days sometimes after 3 minutes).

+1  A: 

Have a look at the Qt documentation for QUdpSocket. There is a note there explaining the class is reentrant. Also from the Qt documentation:

...a class is said to be reentrant if its member functions can be called safely from multiple threads, as long as each thread uses a different instance of the class.

Thus, to answer your question, it does not really matter what the parent of the QThread is, as long as you make sure that the QUdpSocket instance you are using is instantiated within the context of the thread you are using it in.

Ton van den Heuvel
Even by putting QUdpSocket as a class member of the QThread it still wasnt enough to get rid of the intemrittent double free exception it was genrating inside he call to waitForReadyRead()
yan bellavance
Where do you construct the `QUdpSocket` object? Constructing it in the constructor of the thread is not enough as the constructor runs in the context of the main thread. You need to construct it in the `run()` method itself.
Ton van den Heuvel