nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.

Home Page:https://nim-works.github.io/nimskull/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

` .threadvar`s aren't destroyed on thread exit

zerbina opened this issue · comments

Thread-local variables (.threadvar) aren't destroyed when a thread exits, possibly resulting in memory leaks.

Example

type Object = object
  val: int

proc `=destroy`(x: var Object) =
  if x.val > 0:
    echo "destroyed: ", x.val

var tv {.threadvar.}: Object
tv = Object(val: 1) # initialize on the main thread

proc test() =
  proc run() {.thread.} =
    tv = Object(val: 2) # initialize the thread variable on another thread
  
  var t: Thread[void]
  t.createThread(run)
  t.joinThread()

test()

Actual Output

Expected Output

destroyed: 2
destroyed: 1

Possible Solution

The compiler needs to emit a destructor for all thread-local variables in the NimSkull program, which is then called on thread exit.

Fixed by #1308.