Memory leak in event loop inside a thread?

2024-07-15 Thread darkestpigeon
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

Memory leak in event loop inside a thread?

2024-07-15 Thread darkestpigeon
Another piece of info: replacing `std/asyncdispatch` with `chronos` fixes the problem in this case. But the problem returns with the following changes: proc cycler(x: ref MyObj) {.async.} = while true: x.value[] += 1 echo x.value[] await sleepAsync(200)

Memory leak in event loop inside a thread?

2024-07-15 Thread darkestpigeon
Update: after manually clearing dispatcher's `callbacks` and `timers` and manually calling a GC the memory is properly freed, even when the `cycle` coroutine is still pending. So the question is: why do I have to do this manually? Shouldn't this be done automatically when the thread finishes? T

Shared heap and foreign thread interaction

2024-07-17 Thread darkestpigeon
I have a nim function that will be called from a C thread. I'm using mm:orc, and the docs say the the heap is shared in this case. Is it possible to create ref types and store them in nim-managed memory inside such a function? How does such a function interact with heap and GC?

Notifying the dispatcher from a different (foreign) thread

2024-08-15 Thread darkestpigeon
I have some imported C function that looks like `proc doStuff(..., callback: proc(pointer) {.cdecl.}, ctx: pointer)`. It does stuff, and on completion calls the callback with ctx. I know that the callback will be called from another thread. I'd like to wait for completion in a coroutine. Right n

Notifying the dispatcher from a different (foreign) thread

2024-08-16 Thread darkestpigeon
The following setup with `AsyncEvent` seems to work. Not sure how to clean up the event properly, so used `close` and `unregister` (from the nim thread) defensively. Improvements are welcome. type Context = object refCount: int foreignRefCount: int lock: Lock