Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Test2::Tools::Refcount.3pm

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