[PyQt] Keeping references to parent objects

Phil Thompson phil at riverbankcomputing.com
Tue Mar 17 10:36:22 GMT 2015


On 17/03/2015 9:17 am, Florian Bruhin wrote:
> Hi,
> 
> http://stackoverflow.com/a/11945184 says this:
> 
>     - A QObject should generally not have a reference to its parent or
>       any of its ancestors (weak references are ok). This will cause
>       memory leaks at best and occasional crashes as well.
> 
>       [...]
> 
>     - Objects that reference their parent or any ancestor can cause an
>       exit crash.
> 
> Is this true? Is there more information about this somewhere?

It's rubbish.

> I usually use signals, but there are some instances where it'd be
> useful if an object kept a reference to its parent. Should I use
> weakref for that everywhere?

No, just use an ordinary reference. An object keeps an internal 
reference to the parent anyway, one more isn't going to make a 
difference.

The only case where you might have problems is a QObject that is created 
internally by Qt and then exposed through the API. It is missing the 
plumbing used to detect if it gets deleted by C++. However those cases 
are very rare.

Phil


More information about the PyQt mailing list