NAME

Test::Refcount - assert reference counts on objects

SYNOPSIS

use Test::More tests => 2;
use Test::Refcount;

use Some::Class;

my $object = Some::Class->new();

is_oneref( $object, '$object has a refcount of 1' );

my $otherref = $object;

is_refcount( $object, 2, '$object now has 2 references' );

DESCRIPTION

The Perl garbage collector uses simple reference counting during the normal execution of a program. This means that cycles or unweakened references in other parts of code can keep an object around for longer than intended. To help avoid this problem, the reference count of a new object from its class constructor ought to be 1. This way, the caller can know the object will be properly DESTROYed when it drops all of its references to it.

This module provides two test functions to help ensure this property holds for an object class, so as to be polite to its callers.

FUNCTIONS

is_refcount( $object, $count, $name )

Test that $object has $count references to it.

is_oneref( $object, $name )

Assert that the $object has only 1 reference to it.

BUGS

  • Values not in variables

    Code such as

    is_oneref( [] );

    breaks on perl 5.8. Passing a variable (e.g)

    my $array = [];
    is_oneref( $array );

    works fine. This limitation should not affect the behaviour of test scripts that use this module.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>