PDLPorters/pdl

PDL::Core::pdumphash mutates ndarrays

Closed this issue · 1 comments

This reproduces:

my $x = sequence 3; $x->doflow; my $y = $x + 1;
isnt $y->trans_parent, undef, '$y has parent';
isnt PDL::Core::pdumphash($x), undef, 'pdumphash works';
isnt $y->trans_parent, undef, '$y still has parent after pdumphash';

The cause of this, identified using:

make core && perl -Mblib -MPDL -e '$|=1; PDL::Core::set_debugging(1);
  $x = sequence 3; $x->doflow; $y = $x + 1;
  print "BEFORE\n"; $_->dump for $x, $y;
  PDL::Core::pdumphash($x); ' |perl Example/address-pseudonymise |&less

is almost certainly that the intermediate ndarray created in the addition above gets attached to an SV which is returned by $trans->parent. This SV then gets destroyed, which then makes the ndarray look like it's not needed (it had an SV which has now gone away), so it gets destroyed.

This was fixed by adding a state flag when attaching an SV to pdls that didn't have one before, in Core.xs. That was then detected and reversed in DESTROY, without then carrying on to do pdl_destroy.