| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Test2::Tools::Refcount 3perl" |
| 58 |
.TH Test2::Tools::Refcount 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
"Test2::Tools::Refcount" \- assert reference counts on objects |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use Test2::Tools::Refcount; |
| 69 |
\& |
| 70 |
\& use Some::Class; |
| 71 |
\& my $object = Some::Class\->new(); |
| 72 |
\& |
| 73 |
\& is_oneref( $object, \*(Aq$object has a refcount of 1\*(Aq ); |
| 74 |
\& |
| 75 |
\& my $otherref = $object; |
| 76 |
\& |
| 77 |
\& is_refcount( $object, 2, \*(Aq$object now has 2 references\*(Aq ); |
| 78 |
.Ve |
| 79 |
.SH DESCRIPTION |
| 80 |
.IX Header "DESCRIPTION" |
| 81 |
The Perl garbage collector uses simple reference counting during the normal |
| 82 |
execution of a program. This means that cycles or unweakened references in |
| 83 |
other parts of code can keep an object around for longer than intended. To |
| 84 |
help avoid this problem, the reference count of a new object from its class |
| 85 |
constructor ought to be 1. This way, the caller can know the object will be |
| 86 |
properly DESTROYed when it drops all of its references to it. |
| 87 |
.PP |
| 88 |
This module provides two test functions to help ensure this property holds |
| 89 |
for an object class, so as to be polite to its callers. |
| 90 |
.PP |
| 91 |
If the assertion fails; that is, if the actual reference count is different to |
| 92 |
what was expected, either of the following two modules may be used to assist |
| 93 |
the developer in finding where the references are. |
| 94 |
.IP \(bu 4 |
| 95 |
If Devel::MAT is installed, this test module will use it to dump the state |
| 96 |
of the memory after a failure. It will create a \fI.pmat\fR file named the same |
| 97 |
as the unit test, but with the trailing \fI.t\fR suffix replaced with |
| 98 |
\&\fI\-TEST.pmat\fR where \f(CW\*(C`TEST\*(C'\fR is the number of the test that failed (in case |
| 99 |
there was more than one). |
| 100 |
.PP |
| 101 |
See the examples below for more information. |
| 102 |
.SH FUNCTIONS |
| 103 |
.IX Header "FUNCTIONS" |
| 104 |
.SS is_refcount |
| 105 |
.IX Subsection "is_refcount" |
| 106 |
.Vb 1 |
| 107 |
\& is_refcount( $object, $count, $name ) |
| 108 |
.Ve |
| 109 |
.PP |
| 110 |
Test that \f(CW$object\fR has \f(CW$count\fR references to it. |
| 111 |
.SS is_oneref |
| 112 |
.IX Subsection "is_oneref" |
| 113 |
.Vb 1 |
| 114 |
\& is_oneref( $object, $name ) |
| 115 |
.Ve |
| 116 |
.PP |
| 117 |
Assert that the \f(CW$object\fR has only 1 reference to it. |
| 118 |
.SS refcount |
| 119 |
.IX Subsection "refcount" |
| 120 |
.Vb 1 |
| 121 |
\& $count = refcount( $object ) |
| 122 |
.Ve |
| 123 |
.PP |
| 124 |
Returns the reference count of the given object as used by the test functions. |
| 125 |
This is useful for making tests that don't care what the count is before they |
| 126 |
start, but simply assert that the count hasn't changed by the end. |
| 127 |
.PP |
| 128 |
.Vb 3 |
| 129 |
\& use Test2::Tools::Refcount import => [qw( is_refcount refcount )]; |
| 130 |
\& { |
| 131 |
\& my $count = refcount( $object ); |
| 132 |
\& |
| 133 |
\& do_something( $object ); |
| 134 |
\& |
| 135 |
\& is_refcount( $object, $count, \*(Aqdo_something() preserves refcount\*(Aq ); |
| 136 |
\& } |
| 137 |
.Ve |
| 138 |
.SH EXAMPLE |
| 139 |
.IX Header "EXAMPLE" |
| 140 |
Suppose, having written a new class \f(CW\*(C`MyBall\*(C'\fR, you now want to check that its |
| 141 |
constructor and methods are well-behaved, and don't leak references. Consider |
| 142 |
the following test script: |
| 143 |
.PP |
| 144 |
.Vb 2 |
| 145 |
\& use Test::More tests => 2; |
| 146 |
\& use Test2::Tools::Refcount; |
| 147 |
\& |
| 148 |
\& use MyBall; |
| 149 |
\& |
| 150 |
\& my $ball = MyBall\->new(); |
| 151 |
\& is_oneref( $ball, \*(AqOne reference after construct\*(Aq ); |
| 152 |
\& |
| 153 |
\& $ball\->bounce; |
| 154 |
\& |
| 155 |
\& # Any other code here that might be part of the test script |
| 156 |
\& |
| 157 |
\& is_oneref( $ball, \*(AqOne reference just before EOF\*(Aq ); |
| 158 |
.Ve |
| 159 |
.PP |
| 160 |
The first assertion is just after the constructor, to check that the reference |
| 161 |
returned by it is the only reference to that object. This fact is important if |
| 162 |
we ever want \f(CW\*(C`DESTROY\*(C'\fR to behave properly. The second call is right at the |
| 163 |
end of the file, just before the main scope closes. At this stage we expect |
| 164 |
the reference count also to be one, so that the object is properly cleaned up. |
| 165 |
.PP |
| 166 |
Suppose, when run, this produces the following output (presuming |
| 167 |
Devel::MAT::Dumper is available): |
| 168 |
.PP |
| 169 |
.Vb 9 |
| 170 |
\& 1..2 |
| 171 |
\& ok 1 \- One reference after construct |
| 172 |
\& not ok 2 \- One reference just before EOF |
| 173 |
\& # Failed test \*(AqOne reference just before EOF\*(Aq |
| 174 |
\& # at ex.pl line 26. |
| 175 |
\& # expected 1 references, found 2 |
| 176 |
\& # SV address is 0x55e14c310278 |
| 177 |
\& # Writing heap dump to ex\-2.pmat |
| 178 |
\& # Looks like you failed 1 test of 2. |
| 179 |
.Ve |
| 180 |
.PP |
| 181 |
This has written a \fIex\-2.pmat\fR file we can load using the \f(CW\*(C`pmat\*(C'\fR shell and |
| 182 |
use the \f(CW\*(C`identify\*(C'\fR command on the given address to find where it went: |
| 183 |
.PP |
| 184 |
.Vb 10 |
| 185 |
\& $ pmat ex\-2.pmat |
| 186 |
\& Perl memory dumpfile from perl 5.28.1 threaded |
| 187 |
\& Heap contains 25233 objects |
| 188 |
\& pmat> identify 0x55e14c310278 |
| 189 |
\& HASH(0)=MyBall at 0x55e14c310278 is: |
| 190 |
\& ├─(via RV) the lexical $ball at depth 1 of CODE() at 0x55e14c3104a0=main_cv, which is: |
| 191 |
\& │ └─the main code |
| 192 |
\& └─(via RV) value {self} of HASH(2) at 0x55e14cacb860, which is (*A): |
| 193 |
\& └─(via RV) value {cycle} of HASH(2) at 0x55e14cacb860, which is: |
| 194 |
\& itself |
| 195 |
.Ve |
| 196 |
.PP |
| 197 |
(This document isn't intended to be a full tutorial on Devel::MAT and the |
| 198 |
\&\f(CW\*(C`pmat\*(C'\fR shell; for that see Devel::MAT::UserGuide). |
| 199 |
.PP |
| 200 |
From this output, we can see that the constructor was well-behaved, but that a |
| 201 |
reference was leaked by the end of the script \- the reference count was 2, |
| 202 |
when we expected just 1. Reading the trace output, we can see that there were |
| 203 |
2 references that could be found \- one stored in the \f(CW$ball\fR lexical in the main |
| 204 |
program, and one stored in a HASH. Since we expected to find the \f(CW$ball\fR lexical |
| 205 |
variable, we know we are now looking for a leak in a hash somewhere in the |
| 206 |
code. From reading the test script, we can guess this leak is likely to be in |
| 207 |
the \fBbounce()\fR method. Furthermore, we know that the reference to the object |
| 208 |
will be stored in a HASH in a member called \f(CW\*(C`self\*(C'\fR. |
| 209 |
.PP |
| 210 |
By reading the code which implements the \fBbounce()\fR method, we can see this is |
| 211 |
indeed the case: |
| 212 |
.PP |
| 213 |
.Vb 6 |
| 214 |
\& sub bounce |
| 215 |
\& { |
| 216 |
\& my $self = shift; |
| 217 |
\& my $cycle = { self => $self }; |
| 218 |
\& $cycle\->{cycle} = $cycle; |
| 219 |
\& } |
| 220 |
.Ve |
| 221 |
.PP |
| 222 |
From reading the tracing output, we find that the HASH this object is |
| 223 |
referenced in also contains a reference to itself, in a member called |
| 224 |
\&\f(CW\*(C`cycle\*(C'\fR. This comes from the last line in this function, a line that |
| 225 |
purposely created a cycle, to demonstrate the point. While a real program |
| 226 |
probably wouldn't do anything quite this obvious, the trace would still be |
| 227 |
useful in finding the likely cause of the leak. |
| 228 |
.PP |
| 229 |
If \f(CW\*(C`Devel::MAT::Dumper\*(C'\fR is not available, then these detailed traces will not |
| 230 |
be produced. The basic reference count testing will still take place, but a |
| 231 |
smaller message will be produced: |
| 232 |
.PP |
| 233 |
.Vb 7 |
| 234 |
\& 1..2 |
| 235 |
\& ok 1 \- One reference after construct |
| 236 |
\& not ok 2 \- One reference just before EOF |
| 237 |
\& # Failed test \*(AqOne reference just before EOF\*(Aq |
| 238 |
\& # at demo.pl line 16. |
| 239 |
\& # expected 1 references, found 2 |
| 240 |
\& # Looks like you failed 1 test of 2. |
| 241 |
.Ve |
| 242 |
.SH BUGS |
| 243 |
.IX Header "BUGS" |
| 244 |
.IP \(bu 4 |
| 245 |
Temporaries created on the stack |
| 246 |
.Sp |
| 247 |
Code which creates temporaries on the stack, to be released again when the |
| 248 |
called function returns does not work correctly on perl 5.8 (and probably |
| 249 |
before). Examples such as |
| 250 |
.Sp |
| 251 |
.Vb 1 |
| 252 |
\& is_oneref( [] ); |
| 253 |
.Ve |
| 254 |
.Sp |
| 255 |
may fail and claim a reference count of 2 instead. |
| 256 |
.Sp |
| 257 |
Passing a variable such as |
| 258 |
.Sp |
| 259 |
.Vb 2 |
| 260 |
\& my $array = []; |
| 261 |
\& is_oneref( $array ); |
| 262 |
.Ve |
| 263 |
.Sp |
| 264 |
works fine. Because of the intention of this test module; that is, to assert |
| 265 |
reference counts on some object stored in a variable during the lifetime of |
| 266 |
the test script, this is unlikely to cause any problems. |
| 267 |
.SH ACKNOWLEDGEMENTS |
| 268 |
.IX Header "ACKNOWLEDGEMENTS" |
| 269 |
Peter Rabbitson <[email protected]> \- for suggesting using core's \f(CW\*(C`B\*(C'\fR |
| 270 |
instead of \f(CW\*(C`Devel::Refcount\*(C'\fR to obtain refcounts |
| 271 |
.SH AUTHOR |
| 272 |
.IX Header "AUTHOR" |
| 273 |
Paul Evans <[email protected]> |