Cross-reference causes memory leaking in Perl like this.
{
my @a = qw(a b c);
my @b = qw(a b c);
# both reference count are 1
push @a, \@b;
# @b reference count is 2(from @b and via @a)
push @b, \@a;
}
# @b reference count is 2(from via @a)
I understand memory leaking by cross-reference in this situation. But the memory leaking can be resolve by explicit reassignment like this.
{
my @a = qw(a b c);
my @b = qw(a b c);
# both reference count are 1
push @a, \@b;
# @b reference count is 2(from @b and via @a)
push @b, \@a;
@a = ();
}
# why is @b reference count 0?
@a is lexical scope so I think even if there is no reassignment, @a's reference will be invalid but former cause memory leaking and later is not, why?