Date: Wed, 7 Sep 2005 16:21:41 -0300 From: romildo at uber.com.br To: lablgtk at math.nagoya-u.ac.jp Subject: Help in C interface Message-ID: <20050907192141.GA7177 at malaquias> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hello. I am just trying to build an OCaml interface to the gtkdatabox (http://www.eudoxos.de/gtk/gtkdatabox/) C library. This library has the function: gint gtk_databox_data_add (GtkDatabox * box, guint length, gfloat * X, gfloat * Y, GdkColor color, guint type, guint size); which is used to add two arrays of float values (X and Y) to the list of data of the GtkDatabox (these are the data plotted by GtkDatabox). GtkDatabox will not copy or backup the data. The user has to make sure that X and Y pointers will stay valid until the data is removed from the GtkDatabox or until the user destroys the GtkDatabox itself. I am having difficults in making an interface for this function in OCaml, because it seems that it is not possible to construct a C array in OCaml. So the two arrays would be constructed in C. Then I do not know how to control their lifetimes. The following attempt is not good because of the memory leaks: CAMLprim value ml_gtk_databox_data_add(value box, value length, value X, value Y, value color, value type, value size) { const gint n = Int_val(length); double vetx = malloc(n * sizeof float); // memory leak double vety = malloc(n * sizeof float); // memory leak int i; for (i = 0; i < n; i++) { vetx[i] = Float_field(X,i); vety[i] = Float_field(Y,i); } return Val_int(gtk_databox_data_add(GtkDatabox_val(box), n, vetx, vety, GdkColor_val(color), Int_val(type), Int_val(size))); } Note this function can be made better, but the memory leak will be there. Any clues on how to solve this problem? Romildo