Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

What does Test::LeakTrace do?

by TerryBerry (Novice)
on Jan 20, 2018 at 21:53 UTC ( [id://1207595]=perlquestion: print w/replies, xml ) Need Help??

TerryBerry has asked for the wisdom of the Perl Monks concerning the following question:

Could you nice folks clarify what Test::LeakTrace does and what problems it finds? I wonder if I misunderstand because it's giving me errors for very, very simple code. Or maybe there are some very simple things about Perl I've been doing wrong all these years. You decide (then tell me).

Consider this code:

#!/usr/bin/perl -w use strict; use Test::LeakTrace; # test for leak leaktrace { require './AlmostEmpty.pm'; };
AlmostEmpty.pm is, well, almost empty:
1;
Here's the output from running the script:
leaked SCALAR(0x558ad5e37f40) from ./dev.pl line 7.

It seems weird to me to get any kind of memory leak for such a very basic script.

Here's my perl version:

This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-li +nux-gnu-thread-multi (with 56 registered patches, see perl -V for more detail)

I'm running on Ubuntu Linux 17.10.

Replies are listed 'Best First'.
Re: What does Test::LeakTrace do?
by choroba (Cardinal) on Jan 20, 2018 at 22:49 UTC
    You can get more information with the -verbose option:
    leaktrace{ require './AlmostEmpty.pm'; } -verbose;

    And it shows:

    leaked SCALAR(0xa6a8a0) from ./3.pl line 7. 6:leaktrace { 7: require './AlmostEmpty.pm' 8:} -verbose; SV = PV(0xa4ac20) at 0xa6a8a0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0xebe6e0 "./AlmostEmpty.pm"\0 CUR = 16 LEN = 24

    So, the leaked scalar contains the required file name. Why is that? Let's read the documentation:

    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. Thus, to trace true leaks, no_leaks_ok() and leaks_cmp_ok() executes a block more than once.

    And indeed, running no_leaks_ok shows:

    ok 1 - leaks 0 <= 0

    So, it's probably some kind of a global variable. And indeed, compare the output with the one from

    use Devel::Peek; Dump $INC{'./AlmostEmpty.pm'};

    It shows the same scalar. So, it's just the entry in the %INC hash that prevents Perl from including the file once more if you require it again.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1207595]
Front-paged by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found