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

destructor for seq[T] elements not called with gc:refc upon setLen 0 #17186

Closed
timotheecour opened this issue Feb 25, 2021 · 2 comments
Closed

Comments

@timotheecour
Copy link
Member

timotheecour commented Feb 25, 2021

destructor not called for elements in a seq after a shrink via for example setLen 0

Example

when true:
  type Foo = object
    f0: int

  proc `=destroy`(a: var Foo) =
    echo ("dtor", a.f0)

  proc main() =
    block:
      var b: seq[Foo]
      b.add Foo(f0: 1)
      b.add Foo(f0: 2)
    echo "ok2"
    block:
      var b: seq[Foo]
      b.add Foo(f0: 11)
      b.add Foo(f0: 12)
      echo "ok3"
      b.setLen 0
      echo "ok4"
    echo "ok5"
    # GC_fullCollect() # makes no difference
  main()

Current Output

("dtor", 1)
("dtor", 2)
ok2
ok3
ok4
ok5

Expected Output

should output same as with gc:arc
("dtor", 1)
("dtor", 2)
ok2
ok3
("dtor", 12)
("dtor", 11)
ok4
ok5

Additional Information

@timotheecour timotheecour changed the title destructor for seq[T] not called with gc:refc upon setLen 0 destructor for seq[T] elements not called with gc:refc upon setLen 0 Feb 25, 2021
@timotheecour
Copy link
Member Author

note

destructors should work, even for gc:refc, and even for vm and js, otherwise you end up with a language split, where code needs to be rewritten for specific backends.

example use case: auto-closing FILE:

type CFileWrap = object
  cfile: File

proc open2(path: string): CFileWrap =
  result.cfile = open(path)

proc `=destroy`(a: var CFileWrap) =
  if a.cfile != nil: close(a.cfile)

@ringabout
Copy link
Member

refc shouldn't accept dedicated destructor support anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants