closureleak.nim 553 B

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