Calling poll on an dynamically loaded module results in
Traceback (most recent call last)
module.nim(15) init
asyncmacro.nim(393) foo
asyncmacro.nim(43) cb0
asyncfutures.nim(210) callback=
asyncfutures.nim(181) clearCallbacks
gc.nim(271) asgnRef
gc.nim(224) decRef
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
file loader.nim
import options, dynlib
import state
type
InitProc = proc(state:State): State {.nimcall.}
PollProc = proc(state:State): State {.nimcall.}
Module* = object
init*: InitProc
poll*: PollProc
lib: LibHandle
proc loadModule*(path:string): Option[Module] =
let lib = loadLib(path)
if lib != nil:
let initAddr = lib.symAddr("init")
let pollAddr = lib.symAddr("poll")
if initAddr != nil and pollAddr != nil:
let init = cast[InitProc](initAddr)
let poll = cast[PollProc](pollAddr)
var module = Module()
module.init = init
module.poll = poll
result = some module
else:
echo "Could not find procs"
return
var module = loadModule("./libmodule.so").get()
var tststate = State()
tststate.foo = "test"
tststate = module.init(tststate) # register async func
while true:
tststate = module.poll(tststate)
file module.nim
# module (as shared object):
# nim c --app:lib module.nim
import asyncdispatch
import state
proc foo(): Future[void] {.async.} =
echo "foo started"
while true:
await sleepAsync(250)
{.push dynlib exportc.}
proc init*(state: State): State =
asyncCheck foo()
return state
proc poll*(state: State): State =
poll()
return state
{.pop.}
# foo started
# Traceback (most recent call last)
# module.nim(15) init
# asyncmacro.nim(393) foo
# asyncmacro.nim(43) cb0
# asyncfutures.nim(210) callback=
# asyncfutures.nim(181) clearCallbacks
# gc.nim(271) asgnRef
# gc.nim(224) decRef
# SIGSEGV: Illegal storage access. (Attempt to read from nil?)
# Error: execution of an external program failed: '/home/z/loader'
file state.nim
type State* = object
foo*: string
nim c --app:lib module.nim
nim c -r loader.nim
foo started
Traceback (most recent call last)
module.nim(15) init
asyncmacro.nim(393) foo
asyncmacro.nim(43) cb0
asyncfutures.nim(210) callback=
asyncfutures.nim(181) clearCallbacks
gc.nim(271) asgnRef
gc.nim(224) decRef
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed:
'/home/z/nimTinyHttp/bug/loader '
What have i done wrong?