English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Callback.register memory leak
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-12-30 (02:01)
From: Ken McMillan <mcmillan@c...>
Subject: Callback.register memory leak

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

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;
  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