You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I create a C++ class that emulates a closure with function pointer and environment members and a member function that runs them I get a segfault. If I directly apply the closure in a standalone function it succeeds.
Example
{.emit: """/*TYPESECTION*/class ClosureClass{ void (*c)(void*); void* env; public: ClosureClass(void (*c)(void*), void* env) { c = c; env=env; } void run() { (*c)(env); }};void apply(void(*c)(void*), void* env) { (*c)(env); }""".};
typeClosureClass {.importcpp: "ClosureClass".} =objecttypeClosure=proc () {.nimcall.}
procmakeClosure(): proc () =let s ="from closure scope"return (proc () =echo s)
procconstructClosureClass(p:Closure,env:pointer):ptrClosureClass {.importcpp: "new ClosureClass(@)".}
let f =makeClosure()
var c =constructClosureClass(cast[Closure](rawProc f), rawEnv f)
# works fineprocapply(p:Closure,env:pointer) {.importcpp: "apply(@)".}
apply(cast[Closure](rawProc f), rawEnv f)
# segfaultsprocrun(this:ClosureClass) {.importcpp:"#.run()".}
run(c[])
Current Output
from closure scope
Traceback (most recent call last)
/home/deech/Nim/cppclosure.nim(28) cppclosure
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Expected Output
from closure scope
from closure scope
$ nim -v
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-07-06
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: a754160d654f03584088412f51b153ae6a170adb
active boot switches: -d:release
The text was updated successfully, but these errors were encountered:
When I create a C++ class that emulates a closure with function pointer and environment members and a member function that runs them I get a segfault. If I directly apply the closure in a standalone function it succeeds.
Example
Current Output
Expected Output
The text was updated successfully, but these errors were encountered: