From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA25968 for caml-redistribution; Fri, 27 Jun 1997 18:53:26 +0200 (MET DST) 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 OAA19606 for ; Fri, 27 Jun 1997 14:33:16 +0200 (MET DST) Received: from tobago.inria.fr (tobago.inria.fr [128.93.8.21]) by concorde.inria.fr (8.8.5/8.7.3) with ESMTP id OAA13192 for ; Fri, 27 Jun 1997 14:33:16 +0200 (MET DST) Received: (from doligez@localhost) by tobago.inria.fr (8.6.10/8.6.6) id OAA16004 for caml-list@inria.fr; Fri, 27 Jun 1997 14:33:14 +0200 Date: Fri, 27 Jun 1997 14:33:14 +0200 From: Damien Doligez Message-Id: <199706271233.OAA16004@tobago.inria.fr> To: caml-list@inria.fr Subject: Re: GC Bug? Sender: weis > However, in copy, after I call alloc, sometimes >the input parameter will change. Yes, of course. More precisely, the problem is that the input parameter does not change. When you call alloc, it may call the garbage collector to reclaim some memory. The garbage collector can move objects around, so you have to make sure that any pointer to ML values that you have are known to the GC, so it can update them. To this end, you have to use the macros Push_roots and Pop_roots as follows: > value test_copy(value v) > { > type *p, a, b; > value result; + Push_roots(r, 1); + r[0] = v; + #define v r[0] > > p = (type *)v; > a = p[0]; > b = p[1]; > > if (a != 0xdeadbeef || b != 0xfeedbeef){ > invalid_argument("TestMod.copy: value changed"); > > result = alloc(ALLOC_LEN, Abstract_tag); > > /* sometimes v will change after the above alloc call */ > if (a != p[0] || b != p[1]){ > invalid_argument("TestMod.copy: value changed after alloc"); > > p = (type *)result; > p[0] = a; p[1] = b; + Pop_roots(); + #undef v > return result; > } In the next version, this will be slightly easier, with a pair of macros called Begin_roots and End_roots that will allow you to directly designate v as a GC root. This is all documented in the O'Caml documentation, chapter "Interfacing C with Objective Caml", section "Living in harmony with the garbage collector", which you can find at >Can anyone tell me if this is a GC bug, or something I'm doing wrong? >Should I be using Abstract_tag for my abstract chunk of memory, or >some other tag? Thank you for any suggestions. Abstract_tag is the right one. -- Damien