MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <17169.62751.133861.390849 at karryall.dnsalias.org> Date: Sun, 28 Aug 2005 19:32:15 +0200 From: Olivier Andrieu To: "Robert R. Schneck" CC: lablgtk at math.nagoya-u.ac.jp Subject: Re: Custom Tree Models In-Reply-To: <1026.209.145.86.33.1125101422.squirrel at math.berkeley.edu> References: <1026.209.145.86.33.1125101422.squirrel at math.berkeley.edu> Robert R. Schneck [Friday 26 August 2005] : > > I'm interested in implementing a custom tree model in LablGTK2. > I've written the C code necessary to implement the GObject huha and > call back into OCaml to do the actual work. So far, it works fine. > > The tricky part is the GtkTreeIter. I want to have one of the > fields be a Caml value. I gather from a previous post on this list > that this might be problematic for reasons of memory leakage. I'm > guessing the problem is this: some C code creates a GtkTreeIter, > there is an OCaml callback during which a garbage collection is > performed, suddenly the value stored in the GtkTreeIter is moved to > a different location but the GtkTreeIter doesn't find out. Is that > it, or are there going to be other problems? Yes, when you store a caml value (allocated on the heap), you have to register it with the GC using ocaml_register_global_root() otherwise the GC may move the value and the pointer is invalidated. The problem with GtkTreeIter is that these are small struct, usually allocated on the C stack and they are copied by value sometimes. So you cannot use register_global_root() because the C code doesn't tell you when the iter runs out of scope and the copies would not be registered anyway. But you can safely store (caml) integer values in the GtkTreeIter. -- Olivier