Error: (with import segfaults)
free(): invalid pointer
Traceback (most recent call last)
/home/runner/libmtg/src/libmtg/cards.nim(119) test1
SIGABRT: Abnormal termination.
Aborted (core dumped)
Error: execution of an external program failed: '/home/runner/libmtg/tests/test1'
Tip: 5 messages have been suppressed, use --verbose to show them.
tools.nim(36) doCmd
Error: Execution failed with exit code 1
... Command: /home/runner/libmtg/nimble-env/bin/nim c --noNimblePath -d:NimblePkgVersion=0.1.0 --hints:off -r --path:. /home/runner/libmtg/tests/test1
Things to note: The trace back line num is wrong, idk why but segfaults usually get it wrong. How do I fix this?
Nim code:
proc load*(cs: var CardSet) =
let lib = TrackedLibHandle(loadLib(cs.sourcePath))
cs.lib = lib
block:
let loadProc = cast[CardSetLoadProc](lib.checkedSymAddr("load"))
let cards = loadProc()
for key, value in cards:
cs.cards[key] = Card()
var vvalue = value
vvalue.source = addr cs
echo vvalue.eventHandle
if vvalue.eventHandle != "":
let name = cstring(vvalue.eventHandle)
let prc = block:
echo 0
let p = lib.checkedSymAddr(name) # Error
echo 1
cast[EventHandleProc](p)
echo "Inbetween"
vvalue.eventHandleProc = prc
if vvalue.statelessEventHandle != "":
let name = cstring(vvalue.statelessEventHandle)
let prc = cast[EventHandleProc](lib.checkedSymAddr(name))
vvalue.statelessEventHandleProc = prc
cs.cards[key][] = vvalue # Assign ref
Would you explain why? They are already defined like this:
EventHandleProc = proc () {.gcsafe, nimcall.}
CardSetLoadProc = proc (): Table[string, CardObject] {.gcsafe, nimcall.}
And did you mean EventHandleProc? CardSetLoadProc works without any issues.This is described in the manual https://nim-lang.org/2.0.4/manual.html#types-procedural-type.
Apparently this is link to the pragma _stdcall so i'll refer you to the microsoft documentation : https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170 (googling will prob work too).
Would you explain why?
It was only an educated guess. It seems to be some other issue though. If you use a type like ` Table[string, CardObject]` accross DLL boundaries you need to compile against nimrtl.dll etc, I hope you are aware.