Newbie question regarding NIm interfacing C.
I am trying to call the function: int glfwGetError(const char ** description) via the nim-glfw wrapper library. I have the following code:
var msg = allocCStringArray([newString(1024), newString(1024)])
let error = wrapper.getError(msg)
echo fmt"getError: {error}"
if error != 0 and msg[0].addr != nil:
echo fmt"msg={msg[0]}"
echo fmt"msg={msg[1]}"
deallocCStringArray(msg) # causes SIGSEGV
When an error code is generated, it causes a SIGSEGV on the dealloc. The library defines the function as proc getError*(description: cstringArray): int32.
I have the following questions:
TIA
Apologies for the noise but I think I have figured this out.
The GLFW documentation states that: "The retrieved description string is only valid until the next error occurs. This means you must make a copy of it if you want to keep it."
This means it allocates the array's strings. Before deallocation, I must reset the array elements to nil. So the following works:
var msg = allocCStringArray(["", ""])
let error = wrapper.getError(msg)
echo fmt"getError: {error}"
if error != 0 and msg[0].addr != nil:
echo fmt"msg={msg[0]}"
echo fmt"msg={msg[1]}"
for i in 0..2:
msg[i] = nil
deallocCStringArray(msg)It seems this C function returns single C string to the given argument, doesn't take an array of C strings. So I think it should be wrapped like: proc getError*(description: ptr cstring): int32. Then, you can get the error message like:
var msg: cstring
getError(addr msg)
if msg != nil:
echo msg @demotomohiro thanks. Makes sense now.
@Araq Library is in maintenance mode so I will have to make a PR. Thanks.