Test::LeakTrace(3)

๐Ÿ’– Test::LeakTrace(3) ๐Ÿ’–

okay bestie, this is the User Contributed Perl Documentation page for Test::LeakTrace, and it is literally all about tracing memory leaks. no cap, that is the whole vibe. ๐Ÿซถ

๐ŸŒธ NAME ๐ŸŒธ

Test::LeakTrace - Traces memory leaks โœจ

๐ŸŽ€ VERSION ๐ŸŽ€

This document describes Test::LeakTrace version 0.17. ๐Ÿ’…

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

Like, here is the whole group chat of how you use it:

use Test::LeakTrace;

# simple report
leaktrace{
    # ...
};

# verbose output
leaktrace{
    # ...
} -verbose;

# with callback
leaktrace{
    # ...
} sub {
    my($ref, $file, $line) = @_;
    warn "leaked $ref from $file line\n";
};

my @refs = leaked_refs{
    # ...
};
my @info = leaked_info{
    # ...
};

my $count = leaked_count{
    # ...
};

# standard test interface
use Test::LeakTrace;

no_leaks_ok{
    # ...
} 'no memory leaks';

leaks_cmp_ok{
    # ...
} '<', 10;

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

Test::LeakTrace gives you several functions that trace memory leaks. It scans arenas โ€” the memory allocation system โ€” so it can detect any leaked SVs in the blocks you give it. ๐Ÿ‘‘

Leaked SVs are SVs which are not released after the end of the scope they have been created. These SVs include global variables and internal caches. For example, if you call a method in a tracing block, perl might prepare a cache for the method. So, to trace true leaks, no_leaks_ok() and leaks_cmp_ok() executes a block more than once. fr fr, that part matters. ๐Ÿ˜ญ

๐Ÿ”ฅ INTERFACE ๐Ÿ”ฅ

๐Ÿ’… Exported functions

  • leaked_info { BLOCK } โ€” executes BLOCK and returns a list of leaked SVs and places where the SVs come from, i.e. [$ref, $file, $line]. ๐Ÿ•ต๏ธ
  • leaked_refs { BLOCK } โ€” executes BLOCK and returns a list of leaked SVs. โœจ
  • leaked_count { BLOCK } โ€” executes BLOCK and returns the number of leaked SVs. ๐Ÿ”ข
  • leaktrace { BLOCK } ?($mode | \&callback) โ€” executes BLOCK and reports leaked SVs to *STDERR. ๐Ÿ“ข

Defined $modes are:

  • -simple โ€” default. Reports the leaked SV identity (type and address), file name and line number. ๐ŸŒธ
  • -sv_dump โ€” in addition to -simple, dumps the sv content using sv_dump(), which also implements Devel::Peek::Dump(). ๐Ÿ’พ
  • -lines โ€” in addition to -simple, prints suspicious source lines. ๐Ÿ“œ
  • -verbose โ€” both -sv_dump and -lines. ๐Ÿ—ฃ๏ธ
  • no_leaks_ok { BLOCK } ?$description โ€” tests that BLOCK does not leaks SVs. This is a test function using Test::Builder. โœ…

Note that BLOCK is called more than once. This is because BLOCK might prepare caches which are not memory leaks. ๐Ÿซถ

  • leaks_cmp_ok { BLOCK } $cmp_op, $number, ?$description โ€” tests that BLOCK leaks a specific number of SVs. This is a test function using Test::Builder. โœ…

Note that BLOCK is called more than once. This is because BLOCK might prepare caches which are not memory leaks. ๐Ÿซถ

  • count_sv() โ€” counts all the SVs in the arena. ๐Ÿงฎ

๐ŸŽ€ Script interface

Like Devel::LeakTrace, Test::LeakTrace::Script is provided for whole scripts. ๐Ÿฆ‹

The arguments of the use Test::LeakTrace::Script directive are the same as leaktrace(). ๐Ÿ’…

$ TEST_LEAKTRACE=-sv_dump perl -MTest::LeakTrace::Script script.pl
$ perl -MTest::LeakTrace::Script=-verbose script.pl
#!perl
# ...

use Test::LeakTrace::Script sub{
    my($ref, $file, $line) = @_;
    # ...
};

# ...

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

๐Ÿงช Testing modules

Here is a test script template that checks memory leaks. ๐Ÿ‘‘

#!perl -w
use strict;
use constant HAS_LEAKTRACE => eval{ require Test::LeakTrace };
use Test::More HAS_LEAKTRACE ? (tests => 1) : (skip_all => 'require Test::LeakTrace');
use Test::LeakTrace;

use Some::Module;

leaks_cmp_ok{
    my $o = Some::Module->new();
    $o->something();
    $o->something_else();
} '<', 1;

๐Ÿ“ DEPENDENCIES ๐Ÿ“

Perl 5.8.1 or later, and a C compiler. ๐Ÿ”ง

๐Ÿ’€ CAVEATS ๐Ÿ’€

Test::LeakTrace does not work with Devel::Cover and modules which install their own runops routines, or the perl executor. So if the test functions of this module detect strange runops routines, they do nothing and report okay. ๐Ÿ™ˆ

lowkey important: that caveat is not a vibe, it is the actual behavior. ๐Ÿ’€

๐Ÿ› BUGS ๐Ÿ›

No bugs have been reported. ๐ŸŽ‰

Please report any bugs or feature requests to the author. ๐Ÿ“ฌ

๐Ÿ”— SEE ALSO ๐Ÿ”—

  • Devel::LeakTrace.
  • Devel::LeakTrace::Fast.
  • Test::TraceObject.
  • Test::Weak.

For guts:

  • perlguts.
  • perlhack.
  • sv.c.

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Goro Fuji(gfx) <gfuji(at)cpan.org>. ๐Ÿ’Œ

Copyright (c) 2009-2010, Goro Fuji(gfx). All rights reserved. ๐Ÿซถ

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ๐Ÿ’–


signing off: source/version perl v5.42.0, date 2021-01-06, page Test::LeakTrace(3) โ€” mwah ๐Ÿ’‹โœจ

๐Ÿ“ฆ where this page comes from