[PyQt] sip: Member objects and GC

Jonathan Kleinehellefort jk at molb.org
Fri Feb 24 16:13:11 GMT 2012


I was asked to debug memory corruption problem in some sip-based Python
bindings, and what I found leads me to believe that sip is to blame.
I'll try to explain the problem:

Let's say you have a C++ class that looks like this:

    struct C { M m; };

And then let's assume there is some Python code that accesses m after
its containing c object is no longer referenced by Python, for example:


The Python wrapper for m will then point inside the *deleted* c object,
leading to memory corruption.

I have a more complete example at


First of all, I think this should be considered a bug in sip, since it
easily can result in memory corruption, while there is not even a
warning or even a mention of this problem in the documentation.  It
might even be better for sip to refuse generating code for accessing

What I would like happen in this case is that when the m wrapper is
created, the reference count for the c wrapper would be increased, and a
reference to c is stored in m, so that c can be dereferenced when m is

However, I'm not sure how to achieve this.  I looked at the ownership
stuff but I don't think this will help me.  So I'm asking for help on
how to fix or work around this problem.

 -- Jonathan Kleinehellefort

More information about the PyQt mailing list