Skip to content

Clear weakened references when an array slot releases the last owner #1175

Description

@fglock

Summary

Overwriting an array slot that holds the last strong reference does not clear
weakened references in PerlOnJava. This affects both the JVM and interpreter
backends and breaks Perl's observable weak-reference lifetime semantics.

Minimal reproducer

use strict;
use warnings;
use Scalar::Util qw(weaken);

my $object = bless {}, 'Thing';
my $weak = $object;
weaken($weak);

my @array = ($object);
undef $object;
$array[0] = undef;

print defined($weak) ? "retained\n" : "released\n";

Standard Perl prints released. Both PerlOnJava backends print retained.
The same problem occurs when the array is nested in a hash and when the array
reference is first copied into a local variable.

CPAN evidence

Algorithm::SlidingWindow 1.002 failed in CPAN tester run
20260827-171022-1876. Its pure-Perl reference-lifetime test passes under
standard Perl but fails under PerlOnJava:

  • t/refs.t: obj1 freed after eviction (slot cleared) fails on the JVM
    backend.
  • t/refs.t: obj2 freed after clear (slots cleared) fails on both backends.

The distribution stores values in a circular array buffer, explicitly replaces
evicted/cleared slots with undef, and expects that to promptly release the
last strong reference.

Expected behavior

When the final strong reference to a referent is removed through an array-slot
overwrite, any weak references to that referent must immediately become
undefined. This must be true for direct arrays, arrays reached through a hash,
and aliases to an array reference.

Suggested acceptance coverage

  • Add project-owned regression tests for direct and nested array-slot
    replacement with undef after Scalar::Util::weaken.
  • Cover circular-buffer eviction and clearing, matching the stable
    Algorithm::SlidingWindow scenarios.
  • Run those tests on both JVM and interpreter backends.
  • Ensure the release path continues to dispatch DESTROY correctly where the
    referent defines one.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:memoryReference tracking memory leaks and lifecyclebugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions