Sorry, I don't understand it. I have the feeling that your code does basically the same as the code in the initial post.
For my Nim 0.19 on LinuxAMD64 the cstring seems to be allocated fine. May your compiler version include a already fixed bug? Note that generally we use plain Nim strings in our code, Nim strings get converted automatically to cstrings when passed to a proc which expects c string. Can you post the original signature of your iup wrapper getFile() proc.