Transferring ownership of lists of pointers

Phil Thompson phil at riverbankcomputing.com
Tue Feb 28 14:03:22 GMT 2023


On 28/02/2023 01:30, Nyall Dawson wrote:
> On Mon, 27 Feb 2023 at 19:05, Phil Thompson 
> <phil at riverbankcomputing.com> wrote:
> 
>> If by 'list' you mean 'QList' then it should work. As ever a short
>> complete example that demonstrates the problem would help.
> 
> I've done some deeper digging, and I believe the difference is when
> /Transfer/ is used with a list of objects as part of the "parent"s
> constructor.
> 
> I.e. in this example the constructor fails to transfer ownership,
> while "setChildren" does correctly transfer ownership:
> 
> cpp code:
> 
> class MyChild
> {
>    public:
> };
> 
> class MyParent
> {
>   public:
>       MyParent( const QList< MyChild* >& children ) : mChildren( 
> children ) {}
>       ~MyParent() { qDeleteAll( mChildren ); }
>       void setChildren( const QList< MyChild* >& children ) {
> qDeleteAll( mChildren ); mChildren = children; }
>   private:
>     QList< MyChild * > mChildren;
> };
> 
> .sip file:
> 
> class MyChild
> {
>    public:
> };
> 
> class MyParent
> {
>   public:
>       MyParent( const QList< MyChild* >& children /Transfer/ );
>       ~MyParent();
>       void setChildren( const QList< MyChild* >& children /Transfer/ );
> };
> 
> 
> This python code works correctly:
> 
> p = MyParent([])
> p.setChildren([MyChild(), MyChild()])
> del p
> 
> But this crashes:
> 
> p = MyParent([MyChild(), MyChild()])
> del p

This should be fixed in the next SIP snapshot. You will also need to 
update the PyQt5/PyQt6 sip module.

Thanks,
Phil


More information about the PyQt mailing list