Post greatly edited ...
Hi,
I'm getting 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)
assert(mapper != nil, "Process Mapper uninitialised!")
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))
I struggeld, thinking it was an issue with the definition of ProcessIDToAddress, but it seems to be caused by the assert line. If I remove the assert in my code, the error goes away! What is the problem here?
The issue isn't with the assert specifically, but rather because you are comparing your function pointer to nil (see here: https://github.com/nim-lang/Nim/issues/6955). If you want, you can disable the warning by using the {.gcsafe.} pragma:
ProcessIDToAddress* = proc (pid: ProcessID, port: var Port, address: var NotAString): void
{.nimcall, gcsafe.}