Transferring ownership of lists of pointers

Nyall Dawson nyall.dawson at gmail.com
Tue Feb 28 01:30:52 GMT 2023


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

Kind regards,
Nyall


More information about the PyQt mailing list