The code below leaks memory and doesn't print "memory deallocated" (so the
object's destructor isn't called). If the `cycler` coroutine isn't created, no
leak occurs. Am I doing something wrong, or is there a memory manager bug at
play? Tried both `2.0.8` and `2.1.9` nim versions. Compiled with `nim c -r
-d:useMalloc`. Without the `-d:useMalloc` the memory leaks even if "memory
deallocated" is printed.
import std/asyncdispatch
type MyObj = object
value: ptr int
proc `=destroy`(x: MyObj) =
if x.value == nil:
return
deallocShared(cast[pointer](x.value))
echo "memory deallocated"
proc newMyObj(x: int): MyObj =
result.value = cast[ptr int](allocShared0(100_000_000*sizeof(int)))
echo "memory allocated"
let p = cast[ptr UncheckedArray[int]](result.value)
for i in 0..<100_000_000:
p[i] = x + i
proc cycler(x: ref MyObj) {.async.} =
for i in 1..3:
x.value[] += 1
echo x.value[]
await sleepAsync(200)
proc main_async() {.async.} =
let x = new(MyObj)
x[] = newMyObj(3)
let fut = cycler(x)
await sleepAsync(1000)
await fut
proc main() {.thread.} =
echo "thread started"
waitFor(main_async())
echo "thread finished"
while true:
var th = Thread[void]()
createThread(th, main)
joinThreads(th)
Run