[ocaml-ctypes] Binding that works in bytecode, segfaults in native code

Hezekiah M. Carty hez at 0ok.org
Mon Oct 20 11:39:06 BST 2014

On Tue, Oct 14, 2014 at 7:09 AM, Jeremy Yallop <yallop at gmail.com> wrote:
> On 8 October 2014 14:19, Hezekiah M. Carty <hez at 0ok.org> wrote:
>> In the mean time are there any suggestions or techniques you can recommend to
>> help track down the cause of this segfault?
> I think A. Hauptmann's advice elsewhere is worth repeating here, for
> the benefit of anyone who runs into similar issues:
>     "Use Gc.compact, not just Gc.full_major. And put it in a often
> called function of your library code, not only at application level. I
> always overlay any suspicious function with a wrapper of this scheme:
>      let f .. =
>        Gc.compact ();
>        let x = f .. in
>        Gc.compact ();
>        x
>     And then run your application with valgrind. Valgrind did find
> this error instantly."
>     (https://github.com/hcarty/ocaml-gdal/pull/3)

To follow up on the specifics for this case: A finalization/free
function provided by GDAL was freeing memory I had allocated from
ctypes leading to a double free.  As A. Hauptmann pointed out in the
pull request link above, valgrind picked up the mistake and its source
immediately.  For this crash removing the GDAL-provided finalization
function was enough to avoid the issue - OCaml + ctypes were already
freeing everything the finalizer would have freed.

I'm rather embarrassed that I didn't use valgrind in the first place.
Thanks to A Hauptmann for getting me back on track with these


More information about the Ctypes mailing list