[PyQt] Question about ownership

Phil Thompson phil at riverbankcomputing.com
Sat Dec 17 10:31:32 GMT 2016


On 17 Dec 2016, at 10:24 am, Boudewijn Rempt <boud at valdyas.org> wrote:
> 
> I'm writing a wrapper library in C++ around the complex internals of
> Krita, and then wrap that library with sip. What I'm not understanding
> is when something is owned by Python and will be deleted if the refcount
> drops to zero.
> 
> For example:
> 
> class Document {
>    virtual ~/Document() { qDebug() << "destructor called"; }
> private:
>    // wraps a krita internal object
>    KisDocument *document;
> 
> }
> 
> class Krita {
>    Document *createDocument() { return new Document(...); }
>    Krita *instance();
> };
> 
> 
> I expected the following script to call Document's destructor:
> 
> document = Krita.instance().createDocument()
> ...
> document = Krita.instance().createDocument()
> 
> Because the first instance of document goes out of scope when the
> the second instance is created. That doesn't happen, and that means
> that I don't understand something here... Is there a way to get sip
> to signal that the return value of the wrapped createDocument method
> is owned by Python? I thought that sipTransferBack could help here, 
> but I cannot figure out how to add that to the Krita.sip file :-(

createDocument() probably needs the /Factory/ annotation.

Phil


More information about the PyQt mailing list