
On Tue, 18 Apr 2023, PICCA Frederic-Emmanuel wrote:
Hello, I need to call a c method which take a const char *
something like
void hkl_holder_add_axis(const struct holder *holder, const char *name);
during all the life of this object, the const char* should be accessible.
So from haskell I need to create a CString which will not be affected by the garbage collection. The memory should be release only when the given object is freed. or when the program stop.
when I use alloca, the memory is releases and the code segfault.
I think that I could use malloc, but in that case I have a memory leak. (this is not that important in my case, since I create less than 10 of these objects).
You can use allocaBytes, but have to make sure, that the memory lives as long as it is needed. Alternatively you can Foreign.ForeignPtr.mallocForeignPtrBytes. But then, you would access the pointer inside with withForeignPtr or do a touchForeignPtr after the last use of your pointer.