[Interest] question about moveToThread docs

Sze Howe Koh szehowe.koh at gmail.com
Wed Feb 11 00:52:15 CET 2015


On 11 February 2015 at 07:34, Sze Howe Koh <szehowe.koh at gmail.com> wrote:
> Hi,
>
> On 11 February 2015 at 06:48, Alan Ezust <alan.ezust at gmail.com> wrote:
>> from the documentation of QObject::moveToThread():
>>
>>     \note A QObject's member variables \e{do not} automatically become
>>     its children. The parent-child relationship must be set by either
>>     passing a pointer to the child's \l{QObject()}{constructor}, or by
>>     calling setParent(). Without this step, the object's member variables
>>     will remain in the old thread when moveToThread() is called.
>>
>> Is this talking about member variables of type QObject ?
>> As in, "subobjects" ?
>
> It is talking about member variables of type "QObject*" -- pointers to
> QObject. I believe that's the recommended practice.
>
>
>> subobjects of type QObject that also have a parent set, might get destructed
>> twice, but is that what this above paragraph is recommending?
>
> They won't get destructed twice if your member variable is a pointer,
> rather than the full object.

Assuming that you don't try to manually delete it, of course.

And actually, if a member variable is set as the child of the "outer
QObject", then it's safe to store the full object instead of a
pointer, because of the destruction order is well-defined. (i) Member
variables get destroyed first, and only after that does (ii) the
"outer QObject" get destroyed.

When a QObject is destroyed, it removes itself from its parent's list
of children. That's what happens in (i). So, when (ii) happens, the
"outer QObject" does not call its children's destructors a second
time.


Regards,
Sze-Howe



More information about the Interest mailing list