Object.pm

filter: Statementstmt 95.2% 139 / 146 Branchbran 72.4% 42 / 58 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 96.1% 25 / 26 Podpod 100% 4 / 4 totaltotal 87.2% 218 / 250 CC 38 SCAR 16.1
CC
38
Cov
87%
_mk_next
9.0
track
7.3
__ANON__
7.2
CRAP 5.0
« Object/State.pm ↑ Summary
1 package Devel::LeakGuard::Object;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Scalar::Util qw( blessed refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Devel::LeakGuard::Object::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our @EXPORT_OK = qw( track leakstate status leakguard );
17  
18 6 our %OPTIONS = (
19 at_end => 0,
20 stderr => 0
21 );
22  
23 6 our ( %DESTROY_NEXT, %DESTROY_ORIGINAL, %DESTROY_STUBBED, %OBJECT_COUNT,
24 %TRACKED );
25  
26 =encoding utf8
27  
28 =head1 NAME
29  
30 Devel::LeakGuard::Object - Scoped checks for object leaks
31  
32 =head1 VERSION
33  
34 This document describes Devel::LeakGuard::Object version 0.09
35  
36 =cut
37  
38 6 our $VERSION = '0.09';
39  
40 =head1 SYNOPSIS
41  
42 # Track a single object
43 use Devel::LeakGuard::Object;
44 my $obj = Foo::Bar->new;
45 Devel::LeakGuard::Object::track($obj);
46  
47 # Track every object
48 use Devel::LeakGuard::Object qw( GLOBAL_bless );
49  
50 # Track every object, summary at exit
51 use Devel::LeakGuard::Object qw( GLOBAL_bless :at_end );
52  
53 # Track a block of code, warning on leaks
54 leakguard {
55 # your potentially leaky code here
56 };
57  
58 # Track a block of code, die on leaks
59 leakguard {
60 # your potentially leaky code here
61 }
62 on_leak => 'die';
63  
64 =head1 DESCRIPTION
65  
66 This module provides tracking of objects, for the purpose of
67 detecting memory leaks due to circular references or inappropriate
68 caching schemes.
69  
70 It is derived from, and backwards compatible with Adam Kennedy's
71 L<Devel::Leak::Object>. Any errors are mine.
72  
73 It works by overriding C<bless> and adding a synthetic C<DESTROY>
74 method to any tracked classes so that it can maintain a count of blessed
75 objects per-class.
76  
77 Object tracking can be enabled:
78  
79 =over
80  
81 =item * for an individual object
82  
83 =item * for a block of code
84  
85 =item * globally
86  
87 =back
88  
89 =head2 Tracking an individual object
90  
91 Track individual objects like this:
92  
93 use Devel::LeakGuard::Object qw( track );
94  
95 # Later...
96 track( my $obj = Foo->new );
97  
98 =head2 Tracking object leaks in a block of code
99  
100 To detect any object leaks in a block of code:
101  
102 use Devel::LeakGuard::Object qw( leakguard );
103  
104 leakguard {
105 # your code here.
106 };
107  
108 =head2 Tracking global object leaks
109  
110 use Devel::LeakGuard::Object qw( GLOBAL_bless );
111  
112 =head2 Finding out what leaked
113  
114 If you use C<leakguard> (recommended) then by default a warning is
115 thrown when leaks are detected. You can customise this behaviour by
116 passing options to C<leakguard>; see the documentation for L</leakguard>
117 for more information.
118  
119 If you use C<GLOBAL_bless> or C<track> then you can also specify the
120 C<:at_end> option
121  
122 use Devel::LeakGuard::Object qw( GLOBAL_bless :at_end );
123  
124 in which case a summary of leaks will be displayed at program exit.
125  
126 =head2 Load early!
127  
128 C<Devel::LeakGuard::Object> can only track allocations of objects
129 compiled after it is loaded - so load it as early as possible.
130  
131 =head2 What is a leak?
132  
133 This module counts the number of blessed instances of each tracked
134 class. When we talk about a 'leak' what we really mean here is an
135 imbalance in the number of allocated objects across some boundary. Using
136 this definition we see a leak even in the case of expected imbalances.
137  
138 When interpreting the results you need to remember that it may be quite
139 legitimate for certain allocations to live beyond the scope of the code
140 under test.
141  
142 You can use the various options that C<leakguard> supports to filter
143 out such legitimate allocations that live beyond the life of the block
144 being checked.
145  
146 =head2 Performance
147  
148 As soon as C<Devel::LeakGuard::Object> is loaded C<bless> is overloaded.
149 That means that C<bless> gets a little slower everywhere. When not
150 actually tracking the overloaded C<bless> is quite fast - but still
151 around four times slower than the built-in C<bless>.
152  
153 Bear in mind that C<bless> is fast and unless your program is doing a
154 huge amount of blessing you're unlikely to notice a difference. On my
155 machine core bless takes around 0.5 μS and loading
156 C<Devel::LeakGuard::Object> slows that down to around 2 μS.
157  
158 =head1 INTERFACE
159  
160 =cut
161  
162 {
163 6 my $magic = 0;
164  
165 153 ▶ my $plain_bless = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
166 153 my $ref = shift;
167 153 ▶ my $class = @_ ? shift : scalar caller;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 149 4 153
168 153 return CORE::bless( $ref, $class );
169 6 };
170  
171 109 ▶ my $magic_bless = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
172 109 my $ref = shift;
173 109 ▶ my $class = @_ ? shift : scalar caller;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 108 1 109
174 109 my $object = CORE::bless( $ref, $class );
175 109 ▶ unless ( $class->isa( 'Devel::LeakGuard::Object::State' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->isa("Devel::LeakGuard::Object::State")) 88 21 109
176 88 Devel::LeakGuard::Object::track( $object );
177 }
178 109 return $object;
179 6 };
180  
181 8 ▶ sub import {
Subroutine import: called CC=6 SCAR=19.7
182 8 my $class = shift;
183 8 my @args = @_;
184 8 my @import = ();
185  
186 8 ▶ unless ( *CORE::GLOBAL::bless eq $plain_bless ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*CORE::GLOBAL::bless eq $plain_bless) 8 0 8
187 # We don't actually need to install our version of bless here but
188 # it'd be nice if any problems that it caused showed up sooner
189 # rather than later.
190 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 warn "It looks as if something else is already "
192 . "overloading bless; there may be troubles ahead";
193 8 };
194 8 *CORE::GLOBAL::bless = $plain_bless;
195 }
196  
197 8 for my $a ( @args ) {
198 4 ▶ if ( 'GLOBAL_bless' eq $a ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('GLOBAL_bless' eq $a) { } 1 3 4
elsif ($a =~ /^:(.+)$/) { } 0 3 3
199 1 _adj_magic( 1 );
200 }
201 elsif ( $a =~ /^:(.+)$/ ) {
202 0 ▶ croak "Bad option: $1" unless exists $OPTIONS{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Devel::LeakGuard::Object::OPTIONS{$1} 0 0 0
203 0 $OPTIONS{$1}++;
204 }
205 else {
206 3 push @import, $a;
207 }
208 }
209  
210 8 return __PACKAGE__->export_to_level( 1, $class, @import );
211 }
212  
213 43 ▶ sub _adj_magic {
Subroutine _adj_magic: called CC=6 SCAR=18.6
214 43 my $adj = shift;
215 43 my $old_magic = $magic;
216 43 ▶ $magic = 0 if ( $magic += $adj ) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($magic += $adj) < 0 0 43 43
217 {
218 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 43 ▶ if ( $old_magic > 0 && $magic == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_magic > 0 and $magic == 0) { } 21 22 43
elsif ($old_magic == 0 and $magic > 0) { } 22 0 22
Condition66%2/3
$old_magic > 0 and $magic == 0
!ll&&!rl&&r
22021
MC/DC50%1/2
$old_magic > 0 and $magic == 0
$old_magic > 0 $magic == 0
Truth table66%2/3
A and B
A= $old_magic > 0B= $magic == 0
ABresult
0X0
100
111
220 21 *CORE::GLOBAL::bless = $plain_bless;
221 }
222 ▶ elsif ( $old_magic == 0 && $magic > 0 ) {
Condition33%1/3
$old_magic == 0 and $magic > 0
!ll&&!rl&&r
0022
MC/DC0%0/2
$old_magic == 0 and $magic > 0
$old_magic == 0 $magic > 0
Truth table33%1/3
A and B
A= $old_magic == 0B= $magic > 0
ABresult
0X0
100
111
223 22 *CORE::GLOBAL::bless = $magic_bless;
224 }
225 }
226 }
227 }
228  
229 =head2 C<< leakguard >>
230  
231 Run a block of code tracking object creation and destruction and report
232 any leaks at block exit.
233  
234 At its simplest C<leakguard> runs a block of code and warns if leaks
235 are found:
236  
237 leakguard {
238 my $foo = Foo->new;
239 $foo->{me} = $foo; # leak
240 };
241  
242 # Displays this warning:
243 Object leaks found:
244 Class Before After Delta
245 Foo 3 4 1
246 Detected at foo.pl line 23
247  
248 If you really don't want to leak you can die instead of warning:
249  
250 leakguard {
251 my $foo = Foo->new;
252 $foo->{me} = $foo; # leak
253 }
254 on_leak => 'die';
255  
256 If you need to do something more complex you can pass a coderef to the
257 C<on_leak> option:
258  
259 leakguard {
260 my $foo = Foo->new;
261 $foo->{me} = $foo; # leak
262 my $bar = Bar->new;
263 $bar->{me} = $bar; # leak again
264 }
265 on_leak => sub {
266 my $report = shift;
267 for my $pkg ( sort keys %$report ) {
268 printf "%s %d %d\n", $pkg, @{ $report->{$pkg} };
269 }
270 # do something
271 };
272  
273 In the event of a leak the sub will be called with a reference to a
274 hash. The keys of the hash are the names of classes that have leaked;
275 the values are refs to two-element arrays containing the bless count for
276 that class before and after the block so the example above would print:
277  
278 Foo 0 1
279 Bar 0 1
280  
281 =head3 Options
282  
283 Other options are supported. Here's the full list:
284  
285 =over
286  
287 =item C<on_leak>
288  
289 What to do if a leak is detected. May be 'warn' (the default), 'die',
290 'ignore' or a code reference. If C<on_leak> is set to 'ignore' no leak
291 tracking will be performed.
292  
293 =item C<only>
294  
295 If you need to concentrate on a subset of classes use C<only> to limit
296 leak tracking to a subset of classes:
297  
298 leakguard {
299 # do stuff
300 }
301 only => 'My::Stuff::*';
302  
303 The pattern to match can be a string (with '*' as a shell-style
304 wildcard), a C<Regexp>, a coderef or a reference to an array of any of
305 the above. This (improbable) example illustrates all of these:
306  
307 leakguard {
308 # do stuff
309 }
310 only => [
311 'My::Stuff::*',
312 qr{Leaky},
313 sub { length $_ > 20 }
314 ];
315  
316 That would track classes beginning with 'My::Stuff::', containing
317 'Leaky' or whose length is greater than 20 characters.
318  
319 =item C<exclude>
320  
321 To track all classes apart from a few exceptions use C<exclude>. The
322 C<exclude> spec is like an C<only> spec but classes that match will be
323 excluded from tracking.
324  
325 =item C<expect>
326  
327 Sometimes a certain amount of 'leakage' is acceptable. Imagine, for
328 example, an application that maintains a single cached database
329 connection in a class called C<My::DB>. The connection is created on
330 demand and deleted after it has been used 100 times - to be created
331 again next time it's needed.
332  
333 We could use C<exclude> to ignore this class - but then we'd miss the
334 case where something goes wrong and we create 5 connections at a time.
335  
336 Using C<exclude> we can specify that no more than one C<My::DB> should
337 be created or destroyed:
338  
339 leakguard {
340 # do stuff
341 }
342 expect => {
343 'My::DB' => [ -1, 1 ]
344 };
345  
346 =back
347  
348 =cut
349  
350 6 ▶use Devel::Peek;
Subroutine BEGIN: called CC=1 SCAR=0.0
351  
352 20 ▶sub leakguard(&@) {
Subroutine leakguard: called CC=1 SCAR=0.0
Pod documented
353 20 my $block = shift;
354 20 my $leakstate = Devel::LeakGuard::Object::State->new( @_ );
355 20 $block->();
356 20 $leakstate->done();
357 19 return;
358 }
359  
360 =head2 C<< leakstate >>
361  
362 Get the current allocation counts for all tracked objects. If
363 C<GLOBAL_bless> is in force this will include all blessed objects. If
364 you are using the finer-grained tracking tools (L</track> and
365 L</leakguard>) then only allocations that they cover will be included.
366  
367 Returns a reference to a hash with package names as keys and allocation
368 counts as values.
369  
370 =cut
371  
372 46 ▶sub leakstate { return {%OBJECT_COUNT} }
Subroutine leakstate: called CC=1 SCAR=0.0
Pod documented
373  
374 =head2 C<< track >>
375  
376 Track an individual object. Tracking an object increases the allocation
377 count for its package by one. When the object is destroyed the
378 allocation count is decreased by one. Current allocation counts may be
379 retrieved using L</leakstate>.
380  
381 If the object is reblessed into a different package the count for the
382 new package will be incremented and the count for the old package
383 decremented.
384  
385 =cut
386  
387 96 ▶sub track {
Subroutine track: called CC=7 SCAR=19.9
Pod documented
388 96 my $object = shift;
389 96 my $class = blessed $object;
390  
391 96 ▶ carp "Devel::LeakGuard::Object::track was passed a non-object"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 96 96
392 unless defined $class;
393  
394 96 my $address = refaddr $object;
395 96 ▶ if ( $TRACKED{$address} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::LeakGuard::Object::TRACKED{$address}) 1 95 96
396  
397 # Reblessing into the same class, ignore
398 return $OBJECT_COUNT{$class}
399 1 ▶ if $class eq $TRACKED{$address};
Branch: 1 branch
BranchTrueFalseTotal
if $class eq $Devel::LeakGuard::Object::TRACKED{$address} 0 1 1
400  
401 # Reblessing into a different class
402 1 $OBJECT_COUNT{ $TRACKED{$address} }--;
403 }
404  
405 96 $TRACKED{$address} = $class;
406  
407 96 ▶ unless ( $DESTROY_STUBBED{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Devel::LeakGuard::Object::DESTROY_STUBBED{$class}) 21 75 96
408 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
409 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
410  
411 21 ▶ if ( exists ${ $class . '::' }{DESTROY}
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $class . '::' }->{"DESTROY"} and *{$class . '::DESTROY';}{"CODE"}) 0 21 21
Condition50%1/2
exists do { $class . '::' }->{"DESTROY"} and *{$class . '::DESTROY';}{"CODE"}
!ll
210
MC/DC0%0/1
exists do { $class . '::' }->{"DESTROY"} and *{$class . '::DESTROY';}{"CODE"}
exists do { $class . '::' }->{"DESTROY"}
Truth table50%1/2
A
A= exists do { $class . '::' }->{"DESTROY"}
Aresult
00
11
412 0 and *{ $class . '::DESTROY' }{CODE} ) {
413 0 $DESTROY_ORIGINAL{$class} = \&{ $class . '::DESTROY' };
414 }
415  
416 21 $DESTROY_STUBBED{$class} = 1;
417  
418 21 *{"${class}::DESTROY"} = _mk_destroy( $class );
419  
420 21 _mk_next( $class );
421 }
422  
423 96 $OBJECT_COUNT{ $TRACKED{$address} }++;
424 }
425  
426 21 ▶sub _mk_destroy {
Subroutine _mk_destroy: called CC=1 SCAR=0.0
427 21 my $pkg = shift;
428  
429 43 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
430 43 my $self = $_[0];
431 43 my $class = blessed $self;
432 43 my $address = refaddr $self;
433  
434 43 ▶ die "Unexpected error: First param to DESTROY is no an object"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 43 43
435 unless defined $class;
436  
437 # Don't do anything unless tracking for the specific object is set
438 43 my $original = $TRACKED{$address};
439 43 ▶ if ( $original ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original) { } 33 10 43
440  
441 33 ▶ warn "Object class '$class' does",
Branch: 1 branch
BranchTrueFalseTotal
if $class ne $original 0 33 33
442 " not match original $TRACKED{$address}"
443 if $class ne $original;
444  
445 33 $OBJECT_COUNT{$original}--;
446  
447 warn "Object count for $TRACKED{$address}",
448 " negative ($OBJECT_COUNT{$original})"
449 33 ▶ if $OBJECT_COUNT{$original} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::LeakGuard::Object::OBJECT_COUNT{$original} < 0 0 33 33
450  
451 33 delete $TRACKED{$address};
452  
453 0 goto &{ $DESTROY_ORIGINAL{$original} }
454 33 ▶ if $DESTROY_ORIGINAL{$original};
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::LeakGuard::Object::DESTROY_ORIGINAL{$original} 0 33 33
455 }
456 else {
457 10 $original = $class;
458 }
459  
460 # If we don't have the DESTROY_NEXT for this class, populate it
461 43 _mk_next( $original );
462 43 my $super = $DESTROY_NEXT{$original}{$pkg};
463 43 ▶ goto &{"${super}::DESTROY"} if $super;
Branch: 1 branch
BranchTrueFalseTotal
if $super 6 37 43
464 37 return;
465 21 };
466 }
467  
468 64 ▶sub _mk_next {
Subroutine _mk_next: called CC=9 SCAR=22.0
469 64 my $class = shift;
470  
471 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
472 64 ▶ return if $DESTROY_NEXT{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::LeakGuard::Object::DESTROY_NEXT{$class} 41 23 64
473  
474 23 $DESTROY_NEXT{$class} = {};
475  
476 23 my @stack = ( $class );
477 23 my %seen = ( UNIVERSAL => 1 );
478 23 my @queue = ();
479  
480 23 ▶ while ( my $c = pop @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = pop @stack) 37 23 60
481 37 ▶ next if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 0 37 37
482  
483 my $has_destroy
484 = $DESTROY_STUBBED{$c}
485 ? exists $DESTROY_ORIGINAL{$c}
486 37 ▶ : ( exists ${"${c}::"}{DESTROY} and *{"${c}::DESTROY"}{CODE} );
Branch: 1 branch
BranchTrueFalseTotal
$Devel::LeakGuard::Object::DESTROY_STUBBED{$c} ? : 26 11 37
Condition100%2/2
exists do { "${c}::" }->{"DESTROY"} and *{"${c}::DESTROY";}{"CODE"}
!ll
83
MC/DC100%1/1
exists do { "${c}::" }->{"DESTROY"} and *{"${c}::DESTROY";}{"CODE"}
exists do { "${c}::" }->{"DESTROY"}
Truth table100%2/2
A
A= exists do { "${c}::" }->{"DESTROY"}
Aresult
00
11
487  
488 37 ▶ if ( $has_destroy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_destroy) { } 3 34 37
489 3 $DESTROY_NEXT{$class}{$_} = $c for @queue;
490 3 @queue = ();
491 }
492 else {
493 34 push @queue, $c;
494 }
495  
496 37 push @stack, reverse @{"${c}::ISA"};
497 }
498  
499 23 $DESTROY_NEXT{$class}{$_} = '' for @queue;
500 }
501  
502 =head2 C<status>
503  
504 Print out a L<Devel::Leak::Object> style summary of current object
505 allocations. If you
506  
507 use Devel::LeakGuard::Object qw( GLOBAL_bless :at_end );
508  
509 then C<status> will be called at program exit to dump a summary of
510 outstanding allocations.
511  
512 =cut
513  
514 4 ▶sub status {
Subroutine status: called CC=4 SCAR=13.9
Pod documented
515 4 ▶ my $fh = $OPTIONS{stderr} ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$Devel::LeakGuard::Object::OPTIONS{'stderr'} ? : 0 4 4
516 4 print $fh "Tracked objects by class:\n";
517 4 for ( sort keys %OBJECT_COUNT ) {
518 4 ▶ next unless $OBJECT_COUNT{$_}; # Don't list class with count zero
Branch: 1 branch
BranchTrueFalseTotal
unless $Devel::LeakGuard::Object::OBJECT_COUNT{$_} 1 3 4
519 3 print $fh sprintf "%-40s %d\n", $_, $OBJECT_COUNT{$_};
520 }
521 }
522  
523 6 ▶END { status() if $OPTIONS{at_end} }
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::LeakGuard::Object::OPTIONS{"at_end"} 0 6 6
524  
525 6 1;
526  
527 __END__
« Object/State.pm ↑ Summary