| 1 | package Test::Memory::Cycle; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Test::Memory::Cycle - Check for memory leaks and circular memory references | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 1.06 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 8 | our $VERSION = '1.06'; | |||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | Perl's garbage collection has one big problem: Circular references | ||||||||||
| 21 | can't get cleaned up. A circular reference can be as simple as two | ||||||||||
| 22 | references that refer to each other: | ||||||||||
| 23 | |||||||||||
| 24 | my $mom = { | ||||||||||
| 25 | name => "Marilyn Lester", | ||||||||||
| 26 | }; | ||||||||||
| 27 | |||||||||||
| 28 | my $me = { | ||||||||||
| 29 | name => "Andy Lester", | ||||||||||
| 30 | mother => $mom, | ||||||||||
| 31 | }; | ||||||||||
| 32 | $mom->{son} = $me; | ||||||||||
| 33 | |||||||||||
| 34 | C<Test::Memory::Cycle> is built on top of C<Devel::Cycle> to give | ||||||||||
| 35 | you an easy way to check for these circular references. | ||||||||||
| 36 | |||||||||||
| 37 | use Test::Memory::Cycle; | ||||||||||
| 38 | |||||||||||
| 39 | my $object = new MyObject; | ||||||||||
| 40 | # Do stuff with the object. | ||||||||||
| 41 | memory_cycle_ok( $object ); | ||||||||||
| 42 | |||||||||||
| 43 | You can also use C<memory_cycle_exists()> to make sure that you have a | ||||||||||
| 44 | cycle where you expect to have one. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 8 | ▶ | use Devel::Cycle qw( find_cycle find_weakened_cycle ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 8 | ▶ | use Test::Builder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | 8 | my $Test = Test::Builder->new; | |||||||||
| 52 | |||||||||||
| 53 | 7 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 7 | my $self = shift; | |||||||||
| 55 | 7 | my $caller = caller; | |||||||||
| 56 | 8 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 7 | *{$caller.'::memory_cycle_ok'} = \&memory_cycle_ok; | |||||||||
| 58 | 7 | *{$caller.'::memory_cycle_exists'} = \&memory_cycle_exists; | |||||||||
| 59 | |||||||||||
| 60 | 7 | *{$caller.'::weakened_memory_cycle_ok'} = \&weakened_memory_cycle_ok; | |||||||||
| 61 | 7 | *{$caller.'::weakened_memory_cycle_exists'} = \&weakened_memory_cycle_exists; | |||||||||
| 62 | 7 | *{$caller.'::memory_cycle_exists'} = \&memory_cycle_exists; | |||||||||
| 63 | |||||||||||
| 64 | 7 | *{$caller.'::weakened_memory_cycle_ok'} = \&weakened_memory_cycle_ok; | |||||||||
| 65 | 7 | *{$caller.'::weakened_memory_cycle_exists'} = \&weakened_memory_cycle_exists; | |||||||||
| 66 | |||||||||||
| 67 | 7 | $Test->exported_to($caller); | |||||||||
| 68 | 7 | $Test->plan(@_); | |||||||||
| 69 | |||||||||||
| 70 | 7 | return; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | =head1 FUNCTIONS | ||||||||||
| 74 | |||||||||||
| 75 | =head2 C<memory_cycle_ok( I<$reference>, I<$msg> )> | ||||||||||
| 76 | |||||||||||
| 77 | Checks that I<$reference> doesn't have any circular memory references. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 11 | ▶ | sub memory_cycle_ok { | ||||||||
|
Subroutine
memory_cycle_ok: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 11 | my $ref = shift; | |||||||||
| 83 | 11 | my $msg = shift; | |||||||||
| 84 | |||||||||||
| 85 | 11 | my $cycle_no = 0; | |||||||||
| 86 | 11 | my @diags; | |||||||||
| 87 | |||||||||||
| 88 | # Callback function that is called once for each memory cycle found. | ||||||||||
| 89 | 12 | ▶ | my $callback = sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 90 | 12 | my $path = shift; | |||||||||
| 91 | 12 | $cycle_no++; | |||||||||
| 92 | 12 | push( @diags, "Cycle #$cycle_no" ); | |||||||||
| 93 | 12 | foreach (@$path) { | |||||||||
| 94 | 36 | my ($type,$index,$ref,$value) = @$_; | |||||||||
| 95 | |||||||||||
| 96 | 36 | my $str = 'Unknown! This should never happen!'; | |||||||||
| 97 | 36 | my $refdisp = _ref_shortname( $ref ); | |||||||||
| 98 | 36 | my $valuedisp = _ref_shortname( $value ); | |||||||||
| 99 | |||||||||||
| 100 | 36 | ▶ | $str = sprintf( ' %s => %s', $refdisp, $valuedisp ) if $type eq 'SCALAR'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 36 | ▶ | $str = sprintf( ' %s => %s', "${refdisp}->[$index]", $valuedisp ) if $type eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 36 | ▶ | $str = sprintf( ' %s => %s', "${refdisp}->{$index}", $valuedisp ) if $type eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 36 | ▶ | $str = sprintf( ' closure %s => %s', "${refdisp}, $index", $valuedisp ) if $type eq 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 36 | push( @diags, $str ); | |||||||||
| 106 | } | ||||||||||
| 107 | 11 | }; | |||||||||
| 108 | |||||||||||
| 109 | 11 | find_cycle( $ref, $callback ); | |||||||||
| 110 | 11 | my $ok = !$cycle_no; | |||||||||
| 111 | 11 | $Test->ok( $ok, $msg ); | |||||||||
| 112 | 11 | ▶ | $Test->diag( join( "\n", @diags, '' ) ) unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 11 | return $ok; | |||||||||
| 115 | } # memory_cycle_ok | ||||||||||
| 116 | |||||||||||
| 117 | =head2 C<memory_cycle_exists( I<$reference>, I<$msg> )> | ||||||||||
| 118 | |||||||||||
| 119 | Checks that I<$reference> B<does> have any circular memory references. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 13 | ▶ | sub memory_cycle_exists { | ||||||||
|
Subroutine
memory_cycle_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | 13 | my $ref = shift; | |||||||||
| 125 | 13 | my $msg = shift; | |||||||||
| 126 | |||||||||||
| 127 | 13 | my $cycle_no = 0; | |||||||||
| 128 | |||||||||||
| 129 | # Callback function that is called once for each memory cycle found. | ||||||||||
| 130 | 13 | ▶ | my $callback = sub { $cycle_no++ }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 131 | |||||||||||
| 132 | 13 | find_cycle( $ref, $callback ); | |||||||||
| 133 | 13 | my $ok = $cycle_no; | |||||||||
| 134 | 13 | $Test->ok( $ok, $msg ); | |||||||||
| 135 | |||||||||||
| 136 | 13 | return $ok; | |||||||||
| 137 | } # memory_cycle_exists | ||||||||||
| 138 | |||||||||||
| 139 | =head2 C<weakened_memory_cycle_ok( I<$reference>, I<$msg> )> | ||||||||||
| 140 | |||||||||||
| 141 | Checks that I<$reference> doesn't have any circular memory references, but unlike | ||||||||||
| 142 | C<memory_cycle_ok> this will also check for weakened cycles produced with | ||||||||||
| 143 | Scalar::Util's C<weaken>. | ||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 3 | ▶ | sub weakened_memory_cycle_ok { | ||||||||
|
Subroutine
weakened_memory_cycle_ok: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 148 | 3 | my $ref = shift; | |||||||||
| 149 | 3 | my $msg = shift; | |||||||||
| 150 | |||||||||||
| 151 | 3 | my $cycle_no = 0; | |||||||||
| 152 | 3 | my @diags; | |||||||||
| 153 | |||||||||||
| 154 | # Callback function that is called once for each memory cycle found. | ||||||||||
| 155 | 6 | ▶ | my $callback = sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 156 | 6 | my $path = shift; | |||||||||
| 157 | 6 | $cycle_no++; | |||||||||
| 158 | 6 | push( @diags, "Cycle #$cycle_no" ); | |||||||||
| 159 | 6 | foreach (@$path) { | |||||||||
| 160 | 19 | my ($type,$index,$ref,$value,$is_weakened) = @$_; | |||||||||
| 161 | |||||||||||
| 162 | 19 | my $str = "Unknown! This should never happen!"; | |||||||||
| 163 | 19 | my $refdisp = _ref_shortname( $ref ); | |||||||||
| 164 | 19 | my $valuedisp = _ref_shortname( $value ); | |||||||||
| 165 | 19 | ▶ | my $weak = $is_weakened ? 'w->' : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 19 | ▶ | $str = sprintf( ' %s%s => %s', $weak, $refdisp, $valuedisp ) if $type eq 'SCALAR'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 19 | ▶ | $str = sprintf( ' %s%s => %s', $weak, "${refdisp}->[$index]", $valuedisp ) if $type eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 19 | ▶ | $str = sprintf( ' %s%s => %s', $weak, "${refdisp}->{$index}", $valuedisp ) if $type eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | |||||||||||
| 171 | 19 | push( @diags, $str ); | |||||||||
| 172 | } | ||||||||||
| 173 | 3 | }; | |||||||||
| 174 | |||||||||||
| 175 | 3 | find_weakened_cycle( $ref, $callback ); | |||||||||
| 176 | 3 | my $ok = !$cycle_no; | |||||||||
| 177 | 3 | $Test->ok( $ok, $msg ); | |||||||||
| 178 | 3 | ▶ | $Test->diag( join( "\n", @diags, "" ) ) unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | |||||||||||
| 180 | 3 | return $ok; | |||||||||
| 181 | } # weakened_memory_cycle_ok | ||||||||||
| 182 | |||||||||||
| 183 | =head2 C<weakened_memory_cycle_exists( I<$reference>, I<$msg> )> | ||||||||||
| 184 | |||||||||||
| 185 | Checks that I<$reference> B<does> have any circular memory references, but unlike | ||||||||||
| 186 | C<memory_cycle_exists> this will also check for weakened cycles produced with | ||||||||||
| 187 | Scalar::Util's C<weaken>. | ||||||||||
| 188 | |||||||||||
| 189 | =cut | ||||||||||
| 190 | |||||||||||
| 191 | 3 | ▶ | sub weakened_memory_cycle_exists { | ||||||||
|
Subroutine
weakened_memory_cycle_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 192 | 3 | my $ref = shift; | |||||||||
| 193 | 3 | my $msg = shift; | |||||||||
| 194 | |||||||||||
| 195 | 3 | my $cycle_no = 0; | |||||||||
| 196 | |||||||||||
| 197 | # Callback function that is called once for each memory cycle found. | ||||||||||
| 198 | 3 | ▶ | my $callback = sub { $cycle_no++ }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 199 | |||||||||||
| 200 | 3 | find_weakened_cycle( $ref, $callback ); | |||||||||
| 201 | 3 | my $ok = $cycle_no; | |||||||||
| 202 | 3 | $Test->ok( $ok, $msg ); | |||||||||
| 203 | |||||||||||
| 204 | 3 | return $ok; | |||||||||
| 205 | } # weakened_memory_cycle_exists | ||||||||||
| 206 | |||||||||||
| 207 | |||||||||||
| 208 | 8 | my %shortnames; | |||||||||
| 209 | 8 | my $new_shortname = "A"; | |||||||||
| 210 | |||||||||||
| 211 | 110 | ▶ | sub _ref_shortname { | ||||||||
|
Subroutine
_ref_shortname: called CC=6 SCAR=17.9
| |||||||||||
| 212 | 110 | my $ref = shift; | |||||||||
| 213 | 110 | my $refstr = "$ref"; | |||||||||
| 214 | 110 | my $refdisp = $shortnames{ $refstr }; | |||||||||
| 215 | 110 | ▶ | if ( !$refdisp ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 216 | 17 | my $sigil = ref($ref) . " "; | |||||||||
| 217 | 17 | ▶ | $sigil = '%' if $sigil eq "HASH "; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 218 | 17 | ▶ | $sigil = '@' if $sigil eq "ARRAY "; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 219 | 17 | ▶ | $sigil = '$' if $sigil eq "REF "; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | 17 | ▶ | $sigil = '&' if $sigil eq "CODE "; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 221 | 17 | $refdisp = $shortnames{ $refstr } = $sigil . $new_shortname++; | |||||||||
| 222 | } | ||||||||||
| 223 | |||||||||||
| 224 | 110 | return $refdisp; | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | =head1 AUTHOR | ||||||||||
| 228 | |||||||||||
| 229 | Written by Andy Lester, C<< <andy @ petdance.com> >>. | ||||||||||
| 230 | |||||||||||
| 231 | =head1 BUGS | ||||||||||
| 232 | |||||||||||
| 233 | Please report any bugs or feature requests to | ||||||||||
| 234 | C<bug-test-memory-cycle at rt.cpan.org>, or through the web interface at | ||||||||||
| 235 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Memory-Cycle>. | ||||||||||
| 236 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||
| 237 | your bug as I make changes. | ||||||||||
| 238 | |||||||||||
| 239 | =head1 SUPPORT | ||||||||||
| 240 | |||||||||||
| 241 | You can find documentation for this module with the perldoc command. | ||||||||||
| 242 | |||||||||||
| 243 | perldoc Test::Memory::Cycle | ||||||||||
| 244 | |||||||||||
| 245 | You can also look for information at: | ||||||||||
| 246 | |||||||||||
| 247 | =over 4 | ||||||||||
| 248 | |||||||||||
| 249 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 250 | |||||||||||
| 251 | L<http://annocpan.org/dist/Test-Memory-Cycle> | ||||||||||
| 252 | |||||||||||
| 253 | =item * CPAN Ratings | ||||||||||
| 254 | |||||||||||
| 255 | L<http://cpanratings.perl.org/d/Test-Memory-Cycle> | ||||||||||
| 256 | |||||||||||
| 257 | =item * RT: CPAN's request tracker | ||||||||||
| 258 | |||||||||||
| 259 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Memory-Cycle> | ||||||||||
| 260 | |||||||||||
| 261 | =item * Search CPAN | ||||||||||
| 262 | |||||||||||
| 263 | L<http://search.cpan.org/dist/Test-Memory-Cycle> | ||||||||||
| 264 | |||||||||||
| 265 | =back | ||||||||||
| 266 | |||||||||||
| 267 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 268 | |||||||||||
| 269 | Thanks to the contributions of Stevan Little, and to Lincoln Stein for writing Devel::Cycle. | ||||||||||
| 270 | |||||||||||
| 271 | =head1 COPYRIGHT | ||||||||||
| 272 | |||||||||||
| 273 | Copyright 2003-2016 Andy Lester. | ||||||||||
| 274 | |||||||||||
| 275 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 276 | it under the terms of the Artistic License v2.0. | ||||||||||
| 277 | |||||||||||
| 278 | See http://www.perlfoundation.org/artistic_license_2_0 or the LICENSE | ||||||||||
| 279 | file that comes with the Test::Memory::Cycle distribution. | ||||||||||
| 280 | |||||||||||
| 281 | =cut | ||||||||||
| 282 | |||||||||||
| 283 | 8 | 1; | |||||||||