FFI: how to allocate a const char * from haskell

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). I would like you advice in order to solve this properly. thanks for your help. Frederic

If I had to guess, what is happening is that the structure holder uses name without copying it under the hood, so that name is freed but some later method depends on that field being available. In this case you probably want malloc, and you can explicitly free name after freeing holder.
On Apr 18, 2023, at 9:03 AM, 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).
I would like you advice in order to solve this properly.
thanks for your help.
Frederic
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

If I had to guess, what is happening is that the structure holder uses name without copying it under the hood, so that name is freed but some later method depends on that field being available.
you are right I just red the doc of mallocByte and it seems that under the hood it use the c malloc method. So at first I will check that the program does not segfault with a malloced array or char thanks Fred

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.

PICCA Frederic-Emmanuel
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).
Maybe a ForeignPtr can help here: it allows to attach a finalizer, which you can use to free the CString once the object is GCed in Haskell. https://hackage.haskell.org/package/base-4.18.0.0/docs/Foreign-ForeignPtr.ht... -- Albert Krewinkel GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
participants (4)
-
Albert Krewinkel
-
Henning Thielemann
-
PICCA Frederic-Emmanuel
-
Vanessa McHale