Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--gc:arc wrong refcounting #12758

Closed
Araq opened this issue Nov 28, 2019 · 1 comment
Closed

--gc:arc wrong refcounting #12758

Araq opened this issue Nov 28, 2019 · 1 comment
Assignees

Comments

@Araq
Copy link
Member

Araq commented Nov 28, 2019

This program leaks memory under --gc:arc because the newPlus calls are not bound to temporaries that need to be destroyed eventually:

Example

discard """
  outputsub: "no leak: "
"""

type
  TExpr {.inheritable.} = object ## abstract base class for an expression
  PLiteral = ref TLiteral
  TLiteral = object of TExpr
    x: int
    op1: string
  TPlusExpr = object of TExpr
    a, b: ref TExpr
    op2: string

method eval(e: ref TExpr): int {.base.} =
  # override this base method
  quit "to override!"

method eval(e: ref TLiteral): int = return e.x

method eval(e: ref TPlusExpr): int =
  # watch out: relies on dynamic binding
  return eval(e.a) + eval(e.b)

proc newLit(x: int): ref TLiteral =
  new(result)
  result.x = x
  result.op1 = $getOccupiedMem()

proc newPlus(a, b: ref TExpr): ref TPlusExpr =
  new(result)
  result.a = a
  result.b = b
  result.op2 = $getOccupiedMem()

const Limit = when compileOption("gc", "markAndSweep") or compileOption("gc", "boehm"): 5*1024*1024 else: 500_000

for i in 0..100_000:
  var s: array[0..11, ref TExpr]
  for j in 0..high(s):
    s[j] = newPlus(newPlus(newLit(j), newLit(2)), newLit(4))
    if eval(s[j]) != j+6:
      quit "error: wrong result"
  if getOccupiedMem() > Limit: quit("still a leak!")

echo "no leak: ", getOccupiedMem()
@Clyybber Clyybber self-assigned this Nov 28, 2019
Araq added a commit that referenced this issue Dec 1, 2019
@Clyybber
Copy link
Contributor

Clyybber commented Dec 7, 2019

Fixed in 3fbb3bf

@Clyybber Clyybber closed this as completed Dec 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants