Test::LeakTrace#

📦 std

Find the scalars, arrays, hashes and code a block of Perl leaves behind.

Each of the functions runs a block and reports what it allocated and did not free: the values themselves, a count, or a report naming the file and line that created each one. The test functions turn that into a test, so a leak becomes a failing test.

Synopsis#

use Test::More;
use Test::LeakTrace;

no_leaks_ok { my @a = (1 .. 10); my %h = (a => 1) } 'no leaks';

leaks_cmp_ok { my $x; $x = \$x } '<', 2, 'one cycle at most';

my $count = leaked_count { my $x; $x = \$x };      # 1
my @refs  = leaked_refs  { my $x; $x = \$x };      # (\$x)
my @info  = leaked_info  { my $x; $x = \$x };      # ([\$x, $file, $line])

leaktrace { my $x; $x = \$x } -verbose;            # report on STDERR
done_testing;

Reporting modes#

leaktrace BLOCK MODE reports each leaked value on STDERR. -simple, the default, prints its type, address and the line that created it; -lines adds the source lines around that line; -sv_dump dumps the value as Devel::Peek does; -verbose is -lines and -sv_dump together; -silent prints nothing. A code reference as MODE is called once per leaked value with a reference to it, the file and the line.

Test::LeakTrace::Script runs the whole program under a trace: perl -MTest::LeakTrace::Script=-verbose script.pl, or the mode in TEST_LEAKTRACE.

This is a native implementation of the XS half of Test::LeakTrace 0.17; its Test/LeakTrace.pm and Test/LeakTrace/Script.pm are used unchanged. Everything listed above is exported by default.

Differences from upstream#

None.

Modules#