gcleak4.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. outputsub: "no leak: "
  3. retries: 2
  4. """
  5. type
  6. TExpr {.inheritable.} = object ## abstract base class for an expression
  7. PLiteral = ref TLiteral
  8. TLiteral = object of TExpr
  9. x: int
  10. op1: string
  11. TPlusExpr = object of TExpr
  12. a, b: ref TExpr
  13. op2: string
  14. method eval(e: ref TExpr): int {.base.} =
  15. # override this base method
  16. quit "to override!"
  17. method eval(e: ref TLiteral): int = return e.x
  18. method eval(e: ref TPlusExpr): int =
  19. # watch out: relies on dynamic binding
  20. return eval(e.a) + eval(e.b)
  21. proc newLit(x: int): ref TLiteral =
  22. new(result)
  23. result.x = x
  24. result.op1 = $getOccupiedMem()
  25. proc newPlus(a, b: sink(ref TExpr)): ref TPlusExpr =
  26. new(result)
  27. result.a = a
  28. result.b = b
  29. result.op2 = $getOccupiedMem()
  30. const Limit = when compileOption("gc", "markAndSweep") or compileOption("gc", "boehm"): 5*1024*1024 else: 500_000
  31. for i in 0..50_000:
  32. var s: array[0..11, ref TExpr]
  33. for j in 0..high(s):
  34. s[j] = newPlus(newPlus(newLit(j), newLit(2)), newLit(4))
  35. if eval(s[j]) != j+6:
  36. quit "error: wrong result"
  37. if getOccupiedMem() > Limit: quit("still a leak!")
  38. echo "no leak: ", getOccupiedMem()