closureleak.nim 573 B

1234567891011121314151617181920212223242526272829303132333435
  1. discard """
  2. outputsub: "true"
  3. disabled: "32bit"
  4. """
  5. from strutils import join
  6. type
  7. TFoo * = object
  8. id: int
  9. fn: proc(){.closure.}
  10. var foo_counter = 0
  11. var alive_foos = newseq[int](0)
  12. proc free*(some: ref TFoo) =
  13. #echo "Tfoo #", some.id, " freed"
  14. alive_foos.del alive_foos.find(some.id)
  15. proc newFoo*(): ref TFoo =
  16. new result, free
  17. result.id = foo_counter
  18. alive_foos.add result.id
  19. inc foo_counter
  20. for i in 0 .. <10:
  21. discard newFoo()
  22. for i in 0 .. <10:
  23. let f = newFoo()
  24. f.fn = proc =
  25. echo f.id
  26. GC_fullcollect()
  27. echo alive_foos.len <= 3