[Haskell-cafe] FFI nested structs - malloc - free

Proclivis mike at proclivis.com
Tue May 5 03:45:40 UTC 2015


I should have mentioned GHC 7.8.3 Ubuntu 64bit

Sent from my iPad

> On May 4, 2015, at 9:42 PM, Proclivis <mike at proclivis.com> wrote:
> 
> FFI Gurus,
> 
> I created a c2hs FFI of a nested C structure, where struct A has a pointer to a struct B. To do so, I used a malloc, but I am unsure if the memory will be freed when the resulting Ptr is freed.
> 
> The example at this link uses the same technique, so it will serve as an example.
> 
> https://github.com/ifesdjeen/haskell-ffi-tutorial/blob/master/src/Example.hsc
> 
> Line 48 and 51 do the malloc and assign the pointer in the struct, from inside a Storable poke implementation.
> 
> But, there is no explicit free, nor a finalizer.
> 
> Will the memory be freed when the Ptr of the Storable is freed?
> 
> If it is, it implies that some magic keeps track of mallocs inside a poke, and creates finalizers. Or, this example leaks. If it leaks, how do I create a finalizer from inside a poke implementation?
> 
> Mike
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list