Subject: | Wrong REFCNT with Inline::Struct |
Date: | Tue, 23 Sep 2014 21:24:32 +0200 |
To: | bug-Inline-Struct [...] rt.cpan.org |
From: | Heinz Knutzen <heinz.knutzen [...] gmail.com> |
I think that REFCNT isn't handled correctly in Inline::Struct.
REFCNT is set to 1 when the struct is created. That is ok.
But REFCNT is incremented each time a struct member is changed.
Hence the REFCNT isn't 1 when the last reference to the struct is removed.
Therefore the memory of struct isn't reclaimed.
Example program:
============================================
use strict;
use warnings;
use Inline C => <<'END', STRUCTS => 1;
struct Foo {
int inum;
double dnum;
char *str;
};
END
my $o = Inline::Struct::Foo->new();
my ($id) = keys %Inline::Struct::Foo::_map_;
print "REFCNT: $Inline::Struct::Foo::_map_{$id}->{REFCNT}\n";
$o->inum(10);
print "REFCNT: $Inline::Struct::Foo::_map_{$id}->{REFCNT}\n";
$o->dnum(3.1415);
print "REFCNT: $Inline::Struct::Foo::_map_{$id}->{REFCNT}\n";
$o->str('Wazzup?');
print "REFCNT: $Inline::Struct::Foo::_map_{$id}->{REFCNT}\n";
$o->inum(11);
print "REFCNT: $Inline::Struct::Foo::_map_{$id}->{REFCNT}\n";
$o = undef;
============================================
Output:
REFCNT: 1
REFCNT: 2
REFCNT: 3
REFCNT: 4
REFCNT: 5
When adding this line to the DESTROY function of generated XS code:
warn("DESTROY: REFCNT %d FREE %d",SvIV(refcnt), tofree);
and got this additional output:
DESTROY: REFCNT 5 FREE 1.
DESTROY: REFCNT 4 FREE 1 during global destruction.
DESTROY: REFCNT 3 FREE 1 during global destruction.
DESTROY: REFCNT 2 FREE 1 during global destruction.
DESTROY: REFCNT 1 FREE 1 during global destruction.
Used versions:
- Inline-Struct-0.10
- Perl v5.18.2