I'm not familiar with the API, but the thing with returned const char* values is that you don't own the memory the pointer is pointing to, so it can change the value whenever it wants to in proceeding functions, if it doesn't want to continously leak memory by always returning new memory (it doesn't know when you're done with the strings either).
Wrapping const char* as cstring is afaik correct, although you have to watch out that you don't modify the string (as the const is stripped away in the Nim version).
So my conclusion would be that something like this would happen in C as well and the intention is that if you want to keep the returned value you have to copy it somewhere else yourself.
@doofenstein Your explanation makes so much sense! I spent hours debugging this.
And this was the fix :D
proc f_scopetest_nim() {.exportc, dynlib.} =
var
scope = svGetScope()
let
- origScopeName = svGetNameFromScope(scope)
+ origScopeName = $svGetNameFromScope(scope)
see also the thread starting here: https://github.com/nim-lang/Nim/issues/3720#issuecomment-751314222 > How do I express const char* in Nim? Doesn't work for me with clang 3.7.1 either.
which evolved into a good solution for expression something like const char* (and more generally const T*)
@xflywind wrote a blog post: https://dev.to/xflywind/wrap-const-char-in-the-nim-language-53no