I wrote a little test case - it's not real code, but I'd really like to
understand what goes wrong here.
**state.nim** \- compile with `nim c state`
import tables
type
arr* = ref array[128 * 128, int64]
testState* = ref tuple[
mem1: seq[arr],
mem2: Table[string, arr]]
**main.nim** \- compile with `nim c main`
import dynlib, state
type initProc = proc (s: testState) {.nimcall.}
let handle = loadLib("./lib.dll") # repalce with liblib.so on linux
if handle.isNil: quit("Couldn't load DLL")
let init = cast[initProc](handle.symAddr("initialize"))
let s = new testState
init(s)
**lib.nim** \- compile with `nim --app:lib c lib`
import state, tables
proc initialize (s: testState) {.exportc, dynlib, cdecl.} =
s.mem1 = newSeq[arr]()
s.mem2 = initTable[string, arr]()
for i in 0..100:
echo i
s.mem2[$i] = new arr
s.mem1.add(new arr)
On my system this crashes after ~16 iterations due to illegal storage access
with a traceback to gc.nim:growObj or tableimpl.nim:rawInsert.
This does not happen when I move the allocations to main.nim.
Is there a way to do stable heap allocations within procs I sym-linked from a
DLL?
Any pointers are greatly appreciated.