| 1 | package Devel::LeakGuard::Object::State; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp qw( croak carp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Devel::LeakGuard::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use List::Util qw( max ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Devel::LeakGuard::Object::State - Scoped object leak checking | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This document describes Devel::LeakGuard::Object::State version 0.09 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Devel::LeakGuard::Object::State; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Later | ||||||||||||||||||||||||||||
| 29 | my $leakstate = Devel::LeakGuard::Object::State->new( | ||||||||||||||||||||||||||||
| 30 | on_leak => 'die' | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | My::Thing->leaky(); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $leakstate->done; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | A C<Devel::LeakGuard::Object::State> captures the current leakstate of | ||||||||||||||||||||||||||||
| 40 | object allocations within a program. When L</done> is called the saved | ||||||||||||||||||||||||||||
| 41 | allocation leakstate is compared with the current leakstate and any | ||||||||||||||||||||||||||||
| 42 | discrepancies are reported. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head2 C<< new >> | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Create a new C<Devel::LeakGuard::Object::State>. A number of options may | ||||||||||||||||||||||||||||
| 49 | be supplied. To see the full list refer to | ||||||||||||||||||||||||||||
| 50 | L<Devel::LeakGuard::Object/leakguard>. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 22 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 22 | my $class = shift; | |||||||||||||||||||||||||||
| 56 | 22 | my ( $pkg, $file, $line ) = caller; | |||||||||||||||||||||||||||
| 57 | 22 | ▶ | croak "expected a number of key => value options" if @_ % 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 22 | my %opt = @_; | |||||||||||||||||||||||||||
| 60 | 22 | ▶ | my $on_leak = delete $opt{on_leak} || 'warn'; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $opt{'on_leak'} || 'warn'
MC/DC100%1/1
delete $opt{'on_leak'} || 'warn'
delete $opt{'on_leak'}
Truth table100%2/2
A
A= delete $opt{'on_leak'}
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 22 | ▶ | return bless {}, 'Devel::LeakGuard::Object::State::Nop' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | if $on_leak eq 'ignore'; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 21 | Devel::LeakGuard::Object::_adj_magic( 1 ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 21 | my $self | |||||||||||||||||||||||||||
| 68 | = bless { leakstate => Devel::LeakGuard::Object::leakstate() }, | ||||||||||||||||||||||||||||
| 69 | $class; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $self->{on_leak} = $on_leak eq 'die' | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | ? sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $class->_with_report( shift, sub { croak @_ } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 2 | ▶ | : $on_leak eq 'warn' ? sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | $class->_with_report( shift, sub { carp @_ } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 21 | ▶ | : $on_leak; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | croak "on_leak must be a coderef, 'warn' or 'die'" | ||||||||||||||||||||||||||||
| 81 | 21 | ▶ | unless 'CODE' eq ref $self->{on_leak}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 21 | $self->{$_} = delete $opt{$_} for qw( expect only exclude ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 21 | ▶ | croak "invalid option(s): ", sort keys %opt if keys %opt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # print "new $class at $file, $line\n"; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 21 | return $self; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub Devel::LeakGuard::Object::State::Nop::done { } | ||||||||||||||||||||||||||
|
Subroutine
done: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | ▶ | sub _with_report { | ||||||||||||||||||||||||||
|
Subroutine
_with_report: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 3 | my ( $class, $rep, $cb ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | local %Carp::Internal = ( | |||||||||||||||||||||||||||
| 98 | %Carp::Internal, | ||||||||||||||||||||||||||||
| 99 | 'Devel::LeakGuard::Object' => 1, | ||||||||||||||||||||||||||||
| 100 | 'Devel::LeakGuard::Object::State' => 1, | ||||||||||||||||||||||||||||
| 101 | $class => 1 | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 3 | $cb->( | |||||||||||||||||||||||||||
| 105 | "Object leaks found:\n", | ||||||||||||||||||||||||||||
| 106 | $class->_fmt_report( $rep ), "\nDetected" | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub _fmt_report { | ||||||||||||||||||||||||||
|
Subroutine
_fmt_report: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 3 | my ( $class, $rep ) = @_; | |||||||||||||||||||||||||||
| 112 | 3 | my $l = max( 5, map { length $_ } keys %$rep ); | |||||||||||||||||||||||||||
| 113 | 3 | my $fmt = " %-${l}s %6s %6s %6s"; | |||||||||||||||||||||||||||
| 114 | 3 | my @r = sprintf $fmt, 'Class', 'Before', 'After', 'Delta'; | |||||||||||||||||||||||||||
| 115 | 3 | for my $cl ( sort keys %$rep ) { | |||||||||||||||||||||||||||
| 116 | 3 | push @r, sprintf $fmt, $cl, @{ $rep->{$cl} }, | |||||||||||||||||||||||||||
| 117 | 3 | $rep->{$cl}[1] - $rep->{$cl}[0]; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 3 | return join "\n", @r; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 6 | ▶ | sub _make_matcher { | ||||||||||||||||||||||||||
|
Subroutine
_make_matcher: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 123 | 6 | my ( $self, $filter ) = @_; | |||||||||||||||||||||||||||
| 124 | 6 | my @m = (); | |||||||||||||||||||||||||||
| 125 | 6 | ▶ | for my $elt ( 'ARRAY' eq ref $filter ? @$filter : $filter ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 7 | ▶ | unless ( ref $elt ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | my $pat = join '', | ||||||||||||||||||||||||||||
| 128 | 6 | ▶ | map { '*' eq $_ ? '.*?' : quotemeta $_ } split //, $elt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 6 | $elt = qr{^$pat$}; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 7 | ▶ | if ( 'Regexp' eq ref $elt ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | push @m, sub { $_ =~ $elt }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | elsif ( 'CODE' eq ref $elt ) { | ||||||||||||||||||||||||||||
| 136 | 0 | push @m, $elt; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||
| 139 | 0 | croak "Bad filter spec"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 22 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 144 | 22 | local $_ = shift; | |||||||||||||||||||||||||||
| 145 | 22 | for my $m ( @m ) { | |||||||||||||||||||||||||||
| 146 | 24 | ▶ | return 1 if $m->(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 12 | return; | |||||||||||||||||||||||||||
| 149 | 6 | }; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 6 | ▶ | sub _filter { | ||||||||||||||||||||||||||
|
Subroutine
_filter: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 153 | 6 | my ( $self, $filter, $invert, @list ) = @_; | |||||||||||||||||||||||||||
| 154 | 6 | my $m = $self->_make_matcher( $filter ); | |||||||||||||||||||||||||||
| 155 | return $invert | ||||||||||||||||||||||||||||
| 156 | 6 | ? grep { !$m->( $_ ) } @list | |||||||||||||||||||||||||||
| 157 | 6 | ▶ | : grep { $m->( $_ ) } @list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 C<< done >> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Call C<done> at the end of the area of code to be leak-checked. If | ||||||||||||||||||||||||||||
| 163 | allocation imbalances are detected the action taken depends on the | ||||||||||||||||||||||||||||
| 164 | options passed to L</new>. By default a warning is displayed. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 40 | ▶ | sub done { | ||||||||||||||||||||||||||
|
Subroutine
done: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 170 | 40 | local $@; | |||||||||||||||||||||||||||
| 171 | # my ( $pkg, $file, $line ) = caller; | ||||||||||||||||||||||||||||
| 172 | # print "done ", ref $self, " at $file, $line\n"; | ||||||||||||||||||||||||||||
| 173 | 40 | ▶ | return if $self->{done}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 21 | Devel::LeakGuard::Object::_adj_magic( -1 ); | |||||||||||||||||||||||||||
| 176 | 21 | my $leakstate = Devel::LeakGuard::Object::leakstate(); | |||||||||||||||||||||||||||
| 177 | 21 | my %seen = (); | |||||||||||||||||||||||||||
| 178 | 21 | my %report = (); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 21 | for my $class ( sort keys %{ $self->{leakstate} }, %$leakstate ) { | |||||||||||||||||||||||||||
| 181 | 157 | ▶ | next if $seen{$class}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 97 | ▶ | my $before = $self->{leakstate}{$class} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'leakstate'}{$class} || 0
MC/DC100%1/1
$self->{'leakstate'}{$class} || 0
$self->{'leakstate'}{$class}
Truth table100%2/2
A
A= $self->{'leakstate'}{$class}
| |||||||||||||||||||||||||||||
| 183 | 97 | ▶ | my $after = $leakstate->{$class} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$leakstate->{$class} || 0
MC/DC100%1/1
$leakstate->{$class} || 0
$leakstate->{$class}
Truth table100%2/2
A
A= $leakstate->{$class}
| |||||||||||||||||||||||||||||
| 184 | 97 | ▶ | $report{$class} = [ $before, $after ] if $before != $after; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 21 | my @keep = keys %report; | |||||||||||||||||||||||||||
| 188 | 21 | ▶ | return unless @keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | @keep = $self->_filter( $self->{only}, 0, @keep ) | ||||||||||||||||||||||||||||
| 191 | 18 | ▶ | if defined $self->{only}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 18 | ▶ | return unless @keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | @keep = $self->_filter( $self->{exclude}, 1, @keep ) | ||||||||||||||||||||||||||||
| 195 | 18 | ▶ | if defined $self->{exclude}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 18 | ▶ | return unless @keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 18 | ▶ | if ( my $exp = $self->{expect} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 6 | my @k = (); | |||||||||||||||||||||||||||
| 200 | 6 | PKG: for my $pkg ( @keep ) { | |||||||||||||||||||||||||||
| 201 | 24 | ▶ | if ( defined( my $range = $exp->{$pkg} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 5 | ▶ | $range = [ $range, $range ] unless 'ARRAY' eq ref $range; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 5 | my $delta = $report{$pkg}[1] - $report{$pkg}[0]; | |||||||||||||||||||||||||||
| 204 | 5 | ▶ | next PKG if $delta >= $range->[0] && $delta <= $range->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$delta >= $range->[0] and $delta <= $range->[1]
MC/DC100%2/2
$delta >= $range->[0] and $delta <= $range->[1]
$delta >= $range->[0]
$delta <= $range->[1]
Truth table100%3/3
A and B
A= $delta >= $range->[0]B= $delta <= $range->[1]
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 21 | push @k, $pkg; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 6 | @keep = @k; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 18 | ▶ | return unless @keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 18 | my %filtrep = (); | |||||||||||||||||||||||||||
| 212 | 18 | $filtrep{$_} = $report{$_} for @keep; | |||||||||||||||||||||||||||
| 213 | 18 | $self->{on_leak}( \%filtrep ); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 21 | ▶ | sub DESTROY { shift->done } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 6 | 1; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Andy Armstrong C<< <andy@hexten.net> >> | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Currently maintained by Paul Cochrane C<< <paul@peateasea.de> >> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Copyright (c) 2009-2026, Andy Armstrong C<< <andy@hexten.net> >>. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 231 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # vim: expandtab shiftwidth=4 | ||||||||||||||||||||||||||||