我最近一直在对一块特别粗糙的递归XS进行一些泄漏检查,并且当我设法让我的所有引用计数很好地工作时非常高兴。这个Pure Perl编程示例如何泄漏?
想象一下,当我发现一个相对良性的Pure Perl泄漏时,我的震惊!
有人可以告诉我为什么这个看似无害的递归函数像疯子一样泄漏吗? (Linux,Ubuntu 12.10,64位)。
#!/usr/bin/perl
use strict;
use warnings;
use Devel::Leak;
sub annotated_hex {
my $annotated = shift;
if (ref $annotated ne 'HASH') { return '<' . (ref $annotated) . '>'; }
my $hex = '';
if (defined $annotated->{hex}) {
$hex .= $annotated->{hex};
}
if (defined $annotated->{elements}) {
if (ref $annotated->{elements} eq 'ARRAY') {
foreach my $element (@{ $annotated->{elements} }) {
$hex .= &annotated_hex ($element);
}
} else {
$hex .= '<Elements=' . (ref $annotated->{elements}) . '>';
}
}
return $hex;
}
Devel::Leak::NoteSV (my $handle);
my $annotated = { 'hex' => 'a824', 'elements' => [ { 'hex' => '0201', }, ] };
my $annotated_hex = &annotated_hex ($annotated);
undef $annotated_hex;
undef $annotated;
Devel::Leak::CheckSV ($handle);
1;
输出是大量泄漏......
$ perl annotate.pl
new 0x22d8a80 : SV = NULL(0x0) at 0x22d8a80
REFCNT = 1
FLAGS = (PADMY)
new 0x22d8f78 : SV = NULL(0x0) at 0x22d8f78
...[24 leaked entries in total]
这是怎么回事与该?
nit:你为什么用'&调用你的函数?我真的不认为这是问题,但它也似乎并不需要他们(颠覆原型)。 – 2013-04-30 01:40:39
习惯的力量,我从Perl 4开始。:)而且不会影响泄漏。 :( – 2013-04-30 02:43:23