【发布时间】:2013-04-23 20:01:03
【问题描述】:
我最近对一个特别粗糙的递归 XS 进行了一些泄漏检查,当我设法让我的所有引用计数正常工作时,我感到非常高兴。
想象一下当我在一个相对良性的 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:你为什么用
&调用你的函数?我真的不认为这是问题所在,但您似乎也不需要它们(颠覆原型)。 -
习惯的力量,我从 Perl 4 开始。:) 不,它不会影响泄漏。 :(
标签: perl function memory recursion memory-leaks