caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ken McMillan <mcmillan@cadence.com>
To: caml-list@yquem.inria.fr
Subject: Callback.register memory leak
Date: Thu, 29 Dec 2005 18:00:59 -0800	[thread overview]
Message-ID: <43B494DB.20808@cadence.com> (raw)


I had a strange memory leak in an ocaml program that turned out to be
caused by Callback.register.  Perhaps this is a bit unusual, but this
program registered many function closures under the same name.  The
intention was to override the previously registered callback under
that name. However, the previously registered closures remained
referenced and were not garbage collected. The problem is in
caml_register_named_value:


CAMLprim value caml_register_named_value(value vname, value val)
{
  struct named_value * nv;
  char * name = String_val(vname);
  unsigned int h = hash_value_name(name);

  nv = (struct named_value *)
         caml_stat_alloc(sizeof(struct named_value) + strlen(name));
  strcpy(nv->name, name);
  nv->val = val;
  nv->next = named_value_table[h];
  named_value_table[h] = nv;
  caml_register_global_root(&nv->val);
  return Val_unit;
}

Notice that the new value in named_value_table is registered
as a global root, but the previous value, if there is one,
is not removed. Thus the memory leak.

Best regards and thanks for a great programming tool...

Ken McMillan


                 reply	other threads:[~2005-12-30  2:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=43B494DB.20808@cadence.com \
    --to=mcmillan@cadence.com \
    --cc=caml-list@yquem.inria.fr \
    /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).