[Haskell-cafe] FFI: how to allocate a const char * from haskell

Vanessa McHale vamchale at gmail.com
Tue Apr 18 13:41:23 UTC 2023


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 <frederic-emmanuel.picca at synchrotron-soleil.fr> 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.



More information about the Haskell-Cafe mailing list