Skip to content

Commit 4d9a7e8

Browse files
hugoplbeta-ziliani
andauthored
Fix exaö.e WeakRef example by removing ref.value call. (#10846)
The `ref.value` call cause the GC to not collect `ref` until end of scope. Co-authored-by: Beta Ziliani <[email protected]>
1 parent 5b50088 commit 4d9a7e8

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/weak_ref.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
# require "weak_ref"
88
#
99
# ref = WeakRef.new("oof".reverse)
10-
# p ref.value # => "foo"
10+
# p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void)@0x7f83406eafc0>
1111
# GC.collect
12+
# p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void).null>
1213
# p ref.value # => nil
1314
# ```
15+
#
16+
# Note that the collection of objects is not deterministic, and depends on many subtle aspects. For instance,
17+
# if the example above is modified to print `ref.value` in the first print, then the collector will not collect it.
1418
class WeakRef(T)
1519
@target : Void*
1620

0 commit comments

Comments
 (0)