Skip to content

Commit

Permalink
Fix exaö.e WeakRef example by removing ref.value call. (#10846)
Browse files Browse the repository at this point in the history
The `ref.value` call cause the GC to not collect `ref` until end of scope.

Co-authored-by: Beta Ziliani <[email protected]>
  • Loading branch information
hugopl and beta-ziliani committed Jul 1, 2024
1 parent 5b50088 commit 4d9a7e8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/weak_ref.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
# require "weak_ref"
#
# ref = WeakRef.new("oof".reverse)
# p ref.value # => "foo"
# p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void)@0x7f83406eafc0>
# GC.collect
# p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void).null>
# p ref.value # => nil
# ```
#
# Note that the collection of objects is not deterministic, and depends on many subtle aspects. For instance,
# if the example above is modified to print `ref.value` in the first print, then the collector will not collect it.
class WeakRef(T)
@target : Void*

Expand Down

0 comments on commit 4d9a7e8

Please sign in to comment.