Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Troubles with C->Caml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: malc <malc@p...>
Subject: Re: [Caml-list] Troubles with C->Caml
On Sun, 21 Oct 2001, Daniel de Rauglaudre wrote:

> Hi,
> 
> On Sun, Oct 21, 2001 at 03:36:04AM +0400, malc wrote:
> 
> >     result = alloc_small (1, 2);
> >     Field (result, 0) = <some value>;
> >     Field (result, 1) = Extract_exception (res);
> 
> FAQ. When calling allocating functions, the GC may move the values of
> the heap. The variable "res", of type "value", is actually a pointer
> to the heap. After the call to "alloc_small", its pointed value may
> have been moved by the GC, but "res" still points to the old area.

Well, the real code contains all the necessary CAMLxxx decorations, and
yet it still coredumps. And belive me, i read everything i could find
on the subject before posting the question. Furthermore i found that
C code in OCaml distribution is very inconsistent w.r.t CAMLxxx macros,
and its hard to draw any conclusions from there.

> 
> The OCaml documentation explains you this point. You have to add
> specific code in the beginning and at the end of your function to tell
> the GC that you are using some variables of type "value". The functions
> to call have names CAMLparam, CAMLlocal, CAMLreturn, things like that,
> defined in "caml/memory.h".
> 
> Read the documentation carefully because if you don't program it
> correctly, you can get memory fault at any time later. 

value func (value param)
{
  CAMLparam1 (param);
  CAMLlocal2 (result, err);
  char *p = String_val (param);
  void *h = af (p);
  err = yaf ();
  if (some_condition) {
    /* raise exception */
    ...
  } else {
    if (Is_exception_result (err)) {
      result = alloc_small (2, 1);
      Field (result, 0) = (value)h;
      Field (result, 1) = Extract_exception (err);
      CAMLreturn (result);
    }
    result = alloc_small (1, 0);
    Field (result, 0) = (value)h;
    CAMLreturn (result);
  }
}

The decorations are there ditto coredump.
-- 
mailto:malc@pulsesoft.com

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr