[PyQt] causes self to be owned by Qt instead of PyQt ???

Phil Thompson phil at riverbankcomputing.com
Wed Apr 1 09:59:55 BST 2015


On 01/04/2015 7:32 am, redstone-cold wrote:
> 1)"The parent argument, if not None, causes self to be owned by Qt
> instead of PyQt", does owned by PyQt also mean the object owned by
> Python ?

Yes.

> If it is true ,then another question below
> 
> 
> 2)In the doc of sip.setdestroyonexit(destroy) says, "Calling this
> function with a value of False disables the automatic destruction of
> C++ instances and C structures(owned by Python)." ,then which is
> responsible for destroying these C++ instances and C structures? the
> dtor of them ?

Nothing destroys them, the dtors never get called.

> 3)In the doc of sip.setdestroyonexit(destroy) says,"When the Python
> interpreter exits it garbage collects those objects that it can. This
> means that any corresponding C++ instances and C structures owned by
> Python are destroyed. Unfortunately this happens in an unpredictable
> order and so can cause memory faults within the wrapped library. "
> 
> 
> "since Python does not guarantee that objects will be deleted in a
> specific order when it exits. On the other hand, when Qt deletes an
> object, it always tries to delete all its children as well, which will
> normally ensure that objects get deleted in the right order. This is
> especially important when Qt takes ownership of an object, because you
> could end up in a situation where an attempt is made to delete the
> object twice (which will result in a crash)." quoted from here
> http://stackoverflow.com/questions/27131294/error-qobjectstarttimer-qtimer-can-only-be-used-with-threads-started-with-qt
> 
> 
> Since Qt ensure that objects get deleted in the right order, so is it
> better to let the parent argument not be None, thus causes self to be
> owned by Qt instead of PyQt? or just set sip.setdestroyonexit(False) ?

Generally it is a good idea to give QObject instances a parent.

> 4) When I Wrapped the if __name__ == '__main__': part in a function
> main(),the issue went away ,can you explain why ? changed version
> https://bpaste.net/show/36c594a1c82e

Just luck - the order in which Python destroys objects has changed.

Phil


More information about the PyQt mailing list