caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Lukasz Stafiniak <lukstafi@gmail.com>
To: Dominik Brugger <dominikbrugger@gmx.de>
Cc: caml-list@yquem.inria.fr, Dominik Brugger <dominikbrugger@fastmail.fm>
Subject: Re: [Caml-list] Re: Ocaml C interface - Usage of custom blocks
Date: Thu, 22 Sep 2005 20:33:14 +0200	[thread overview]
Message-ID: <4a708d205092211334a58abfa@mail.gmail.com> (raw)
In-Reply-To: <87y85pgxhh.fsf@gmx.de>

2005/9/22, Dominik Brugger <dominikbrugger@gmx.de>:
> 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.

Lukasz


  reply	other threads:[~2005-09-22 18:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-22 11:20 Dominik Brugger
2005-09-22 18:33 ` Lukasz Stafiniak [this message]
2005-09-23  7:54   ` [Caml-list] " Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a708d205092211334a58abfa@mail.gmail.com \
    --to=lukstafi@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=dominikbrugger@fastmail.fm \
    --cc=dominikbrugger@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).