문제가 Perl 코드에있는 경우 자체 또는 부모 노드를 가리키는 참조가있을 수 있습니다.
다음은이 동작을 보여주는 간단한 코드 샘플입니다.
{
my @a;
@a = [\@a];
}
일반적으로 부모 개체를 참조하는 개체 형태로 제공됩니다.
{ package parent;
sub new{ bless { 'name' => $_[1] }, $_[0] }
sub add_child{
my($self,$child_name) = @_;
my $child = child->new($child_name,$self);
$self->{$child_name} = $child; # saves a reference to the child
return $child;
}
}
{ package child;
sub new{
my($class,$name,$parent) = @_;
my $self = bless {
'name' => $name,
'parent' => $parent # saves a reference to the parent
}, $class;
return $self;
}
}
{
my $parent = parent->new('Dad');
my $child = parent->add_child('Son');
# At this point both of these are true
# $parent->{Son}{parent} == $parent
# $child->{parent}{Son} == $child
# Both of the objects **would** be destroyed upon leaving
# the current scope, except that the object is self-referential
}
# Both objects still exist here, but there is no way to access either of them.
가장 좋은 방법은 Scalar::Util::weaken입니다.
use Scalar::Util qw'weaken';
{ package child;
sub new{
my($class,$name,$parent) = @_;
my $self = bless {
'name' => $name,
'parent' => $parent
}, $class;
weaken ${$self->{parent}};
return $self;
}
}
모든 가능한 경우 하위 개체의 참조를 삭제하는 것이 좋습니다.
어떤 버전의 Perl 및 DBI입니까? 한 번에 새로운 준비된 성명서를 만들려고 할 때마다 기억 누출에 빠지기는했지만, 그렇게 나쁜 것은 아닙니다. – fennec
CGI 스크립트를 한 번 호출 할 때 메모리가 폭발합니까? 아니면 결국 실패 할 때까지 반복되는 호출이 계속 증가합니까? –