Hi,
I can't seem to get around this error:
Error: 'mapPID2Address' is not GC-safe as it accesses 'mapper' which is a
global using GC'ed memory
with code that basically does this:
import asyncdispatch
import asyncnet
type
ProcessID* = uint32
NotAString = array[256,char]
ProcessIDToAddress* = proc (pid: ProcessID, port: var Port, address: var
NotAString): void {.nimcall.}
var myProcessMapper: pointer
var myClusterPort: uint16
proc mapPID2Address(pid: ProcessID): (string, Port) {.gcSafe.} =
let mapper = cast[ProcessIDToAddress](myProcessMapper)
var port = Port(myClusterPort)
var address: NotAString
mapper(pid, port, address)
let alen = address.find(char(0)) + 1
var address2 = newStringOfCap(alen)
for i in 0..alen:
address2.add(address[i])
result = (address2, port)
proc openPeerConnection(pid: ProcessID): Future[AsyncSocket] {.async.} =
## Opens a connection to another peer.
let (address, port) = mapPID2Address(pid)
result = await asyncnet.dial(address, port)
asyncCheck openPeerConnection(ProcessID(0))
Unfortunately, this example doesn't produce the error; I can't see where the
difference is, but "mapper(pid, port, address)" is what bothers the compiler in
my real code.
Initially, I wanted ProcessIDToAddress to return a string, but after getting
the error, I decided to use an array as var parameter. My understanding is that
the array is a stack object, and not a GC object, and so mapper doesn't
actually access any GCed memory, and should not produce the error.