[Qt-interest] Delete QObjects which are moved to different thread
Wilhelm
wilhelm.meier at fh-kl.de
Wed Oct 26 11:33:34 CEST 2011
Am 26.10.2011 11:16, schrieb Wilhelm:
> Am 26.10.2011 09:37, schrieb Giuseppe D'Angelo:
>> On 26 October 2011 09:34, Giuseppe D'Angelo<dangelog at gmail.com> wrote:
>>> On 26 October 2011 09:27, Wilhelm<wilhelm.meier at fh-kl.de> wrote:
>>>> Hi,
>>>>
>>>> if I move a QObject to a different QThread and then destroy this object
>>>> deferred, the dtor is called by this thread, if it is running: ok.
>>>>
>>>> But if the other thread is not running, I can't use deferred deletion
>>>> since there is no running event loop.
>>>> Trying to delete the object from the main thread via delete operator,
>>>> the dtor ist called from a different thread than the main thread: I
>>>> assume the QObject-dtor detects that the object belongs to a different
>>>> thread and if that is not running it starts a new thread to execute the
>>>> dtor. Right?
>>>
>>> Wrong.
>>
>> Sorry -- I hit the "send" button instead of the "save" button. :)
>>
>> Still, it's wrong. The object gets deleted from the main thread, and
>> that's something you shouldn't do (QObjects are reentrant and not
>> thread safe -- they're supposed to be accessed only from the thread
>> they're living in).
>
> see the attached minimal example producing the following output:
>
> Test::Test(QObject*) QThread(0x25231a0)
> Worker::Worker(QObject*) QThread(0x25231a0)
> void Test::test() QThread(0x25231a0)
> virtual Worker::~Worker() QThread(0x2534390)
>
> if the thread isn't started!
Ok, sometimes asking a question is the answer ...
Sorry, I confused "living" thread (QObject::thread()) and "executing"
thread (QObject::thread()->currentThread()).
Thanks anyway!
>
>>
>> Why did you move the object in the first place if you don't have a
>> running thread with a running event loop? Or, if that thread has
>> finished, why didn't you clean up the objects living in it?
>
> it was just curiousity!
>
>>
>> Cheers,
>> --
>> Giuseppe D'Angelo
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
--
Wilhelm
More information about the Qt-interest-old
mailing list