caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: malc <malc@pulsesoft.com>
To: Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Troubles with C->Caml
Date: Sun, 21 Oct 2001 16:27:08 +0400 (MSD)	[thread overview]
Message-ID: <Pine.LNX.4.21.0110211611550.477-100000@oyster> (raw)
In-Reply-To: <20011021052337.A1404@verdot.inria.fr>

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


  reply	other threads:[~2001-10-21 12:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-20 23:36 malc
2001-10-21  3:23 ` Daniel de Rauglaudre
2001-10-21 12:27   ` malc [this message]
2001-10-21 13:10     ` Dmitry Bely
2001-11-05 17:56     ` Joerg Czeranski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0110211611550.477-100000@oyster \
    --to=malc@pulsesoft.com \
    --cc=caml-list@inria.fr \
    --cc=daniel.de_rauglaudre@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).