caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Callback.register memory leak
@ 2005-12-30  2:00 Ken McMillan
  0 siblings, 0 replies; only message in thread
From: Ken McMillan @ 2005-12-30  2:00 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-12-30  2:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-30  2:00 Callback.register memory leak Ken McMillan

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).