Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: Ocaml C interface - Usage of custom blocks
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-09-22 (18:33)
From: Lukasz Stafiniak <lukstafi@g...>
Subject: Re: [Caml-list] Re: Ocaml C interface - Usage of custom blocks
2005/9/22, Dominik Brugger <>:
> Is there a "best practice" for returning C data to OCaml
> which was allocated by malloc?

My bet is on custom blocks.

> But section 18.2.3 of the manual points out, that it is potentially
> dangerous to free C data, as it might be reclaimed by the OCaml GC.

No. It is dangerous to free C data, because you might still use them
on the OCaml side. GC will not reclaim "malloc pointers".

> So what happens if the data is never explicitly freed? Does the OCaml heap
> space grow until there is no more memory available to the C part of the
> OCaml program?

The data (allocated by malloc) is not on OCaml heap. You have memory
leak on the C side.
> In my opinion the only way to avoid these problems is the usage of
> OCaml custom blocks.

My too. The difficulty comes, when the data is also refered by other C
data structures. I've solved it by reference counters, decremented
explicitly on C side and by finalisation on OCaml side.