tgcleak4.nim 1.2 KB

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