From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA23570; Thu, 29 May 2003 03:34:13 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA23561 for ; Thu, 29 May 2003 03:34:11 +0200 (MET DST) Received: from dirty.research.bell-labs.com (ns1.research.bell-labs.com [204.178.16.6]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4T1Y9H03664 for ; Thu, 29 May 2003 03:34:09 +0200 (MET DST) Received: from grubby.research.bell-labs.com (H-135-104-2-9.research.bell-labs.com [135.104.2.9]) by dirty.research.bell-labs.com (8.12.9/8.12.9) with ESMTP id h4T1Y5Ha038801; Wed, 28 May 2003 21:34:05 -0400 (EDT) Received: from mcs.research.bell-labs.com (mcs.research.bell-labs.com [135.104.32.15]) by grubby.research.bell-labs.com (8.12.9/8.12.9) with ESMTP id h4T1Xxc6008803; Wed, 28 May 2003 21:33:59 -0400 (EDT) Received: from dhcp50114.cs.bell-labs.com (dhcp50114.cs.bell-labs.com [135.104.50.114]) by mcs.research.bell-labs.com (8.12.9/8.12.9) with ESMTP id h4T1Xsf4441110; Wed, 28 May 2003 21:33:54 -0400 (EDT) Subject: Re: [Caml-list] Question about register_global_root From: Jerome Simeon To: Maas-Maarten Zeeman Cc: "Mary F. Fernandez" , caml-list@inria.fr In-Reply-To: <3ED51D27.3000301@wanadoo.nl> References: <3ED511E5.8060401@research.att.com> <3ED51D27.3000301@wanadoo.nl> Content-Type: text/plain Organization: Lucent Technologies Message-Id: <1054171870.1621.23.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.4 Date: 28 May 2003 21:31:10 -0400 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; jerome:01 simeon:01 caml-list:01 bug:01 malloc:01 callback:01 sizeof:01 alloc:01 val:01 crashing:01 ocaml:01 caml:01 garbage:01 referenced:02 unit:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Thanks Maas, We managed to pin down that *!@#! bug after all. You mail was very helpful. - Jerome On Wed, 2003-05-28 at 16:33, Maas-Maarten Zeeman wrote: > Hello, > > I'm by far no Caml expert, but it works pretty well for me in > OcamlExpat, an OCaml wrapper for Expat I wrote a while back. I use a > malloced value to store a pointer to a tuple, which is used to store > event handlers (ocaml functions). > > This is what I did: > > /* Malloc a value for a tuple which will contain the callback > * handlers and register it as global root. > */ > handlers = (value *) malloc(sizeof(value)); > register_global_root(handlers); > > *handlers = alloc_tuple(NUM_HANDLERS); > for(i = 0; i < NUM_HANDLERS; i++) { > Field(*handlers, i) = Val_unit; > } > > One thing to keep in mind is that you have to be really careful, and use > the right CAMLxxxx macro in the right place, otherwise things start > crashing at seemingly random times. The reason for this is that the > global root will correctly stay at the same memory location, but any > ocaml values stored in the root can move during a garbage collection > cycle. If you do not use the CAMLxxxx macro's correctly the values will > change, and move in the middle of your c-function, which almost always > results in a crashing program. > > It is very easy to make mistakes here, but there is a way to (sort of) > test if your c-extension behaves correctly. If you call "Gc.full_major > ()" in your tests you will know almost instantly if there is a problem > related to garbage collection, and possible incorrect use of CAMLxxxx > macros (at least that is the idea). > > Regards, > > Maas > > > Hi Caml experts, > > We are using register_global_root to register addresses in the malloc'd > > C heap that contain Caml values. We noticed that register_global_root > > (comment below) expects a global C variable, but we assumed that it > > should > > work with malloc'd addresses as well. Is there any reason that it > > would not? > > > > /* [register_global_root] registers a global C variable as a memory root > > for the duration of the program, or until [remove_global_root] is > > called. */ > > > > We are getting (somewhat random) core dumps after calling the same Caml > > function repeatedly on the same Caml value (which is referenced > > from the C heap). > > > > Thanks, > > Mary > > -- Jerome Simeon Lucent Technologies ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners