Date: Fri, 27 Jun 1997 14:33:14 +0200
From: Damien Doligez <Damien.Doligez@inria.fr>
Message-Id: <199706271233.OAA16004@tobago.inria.fr>
To: caml-list@inria.fr
Subject: Re: GC Bug?
> 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
<http://pauillac.inria.fr/ocaml/htmlman/node15.html>
>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
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:11 MET