When a lexical variable goes out of scope, as in
{
my %lexical_variable;
...
}
# no longer in scope here
it is supposed to disappear as far as Perl code can tell. That the
same SV is reused the next time that scope is entered is an implement-
ation detail.
The move of hashes’ back-references from magic into the HvAUX struc-
ture in 5.10 caused this implementation detail to leak through.
Normally, weak references to pad variables going out of scope are
killed off:
{
my $scalar;
weaken ($global_scalar = \$scalar);
}
# here $global_scalar is undef
When hashes’ back-references were moved, leave_scope was not updated
to account. (For non-hash variables, it’s the mg_free call that takes
care of it.) So in this case:
{
my %hash;
weaken ($global_scalar = \%hash);
}
$global_scalar would still reference a hash, but one marked PADSTALE.
Modifications to that hash through the reference would be visible the
next time the scope was entered.
av_clear(MUTABLE_AV(sv));
break;
case SVt_PVHV:
+ Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
hv_clear(MUTABLE_HV(sv));
break;
case SVt_PVCV:
use strict;
-plan tests => 10;
+plan tests => 11;
my %h;
delete $h{k}; # must be in void context to trigger the bug
ok $normal_exit, 'freed hash elems are not visible to DESTROY';
}
+
+# Weak references to pad hashes
+{
+ skip_if_miniperl("No Scalar::Util::weaken under miniperl", 1);
+ my $ref;
+ require Scalar::Util;
+ {
+ my %hash;
+ Scalar::Util::weaken($ref = \%hash);
+ 1; # the previous statement must not be the last
+ }
+ is $ref, undef, 'weak refs to pad hashes go stale on scope exit';
+}