Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in C-interface? #2609

Closed
vicuna opened this issue Nov 16, 2000 · 2 comments
Closed

Change in C-interface? #2609

vicuna opened this issue Nov 16, 2000 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Nov 16, 2000

Original bug ID: 221
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hello,

it seems that the C-interface to OCaml has changed slightly in an
unexpected way. I want to raise an exception that takes two arguments
from C. Xavier had sent me the code snippet to do this quite a while
ago, and it used to work fine last time I tested this code part (very
seldom executed).

Can anybody tell me what's different now / what I am doing wrong? Below
are two short snippets to try this out. Compile them with:

ocamlc -custom bla_c.c bla.ml -o bla

and run "bla" (crashes on my machine - a Pentium III / Linux). I haven't
tried it with an older OCaml-release yet, only with OCaml-3.00.

file: bla_c.c

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>

void raise_with_two_args(value tag, value arg1, value arg2)
{
value exc;

/* Protects tag, arg1 and arg2 from being reclaimed by the garbage
collector when the exception value is allocated */
Begin_roots3(tag, arg1, arg2);
exc = alloc_small(3, tag);
Field(exc, 1) = arg1;
Field(exc, 2) = arg2;
End_roots();

mlraise(exc);
}

void f () {
raise_with_two_args(*caml_named_value("Test"),
copy_string("foo"),
Val_int(42));
}

file: bla.ml

external f : unit -> unit = "f"

exception Test of string * int

let main () =
Callback.register_exception "Test" (Test ("", 0));
f ()

let _ = Printexc.catch main ()

Best regards,
Markus Mottl

--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl

@vicuna
Copy link
Author

vicuna commented Nov 16, 2000

Comment author: administrator

it seems that the C-interface to OCaml has changed slightly in an
unexpected way. I want to raise an exception that takes two arguments
from C. Xavier had sent me the code snippet to do this quite a while
ago, and it used to work fine last time I tested this code part (very
seldom executed).

void raise_with_two_args(value tag, value arg1, value arg2)
{
value exc;

/* Protects tag, arg1 and arg2 from being reclaimed by the garbage
collector when the exception value is allocated */
Begin_roots3(tag, arg1, arg2);
exc = alloc_small(3, tag);
Field(exc, 1) = arg1;
Field(exc, 2) = arg2;
End_roots();

mlraise(exc);
}

I'm surprised this ever worked at all! (But I won't go into my old
mail to see if this is really the code I sent you :-).)
Try the following instead:

Begin_roots3(tag, arg1, arg2);
exc = alloc_small(3, 0);
Field(exc, 0) = tag;
Field(exc, 1) = arg1;
Field(exc, 2) = arg2;
End_roots();

And let us know how it goes. Cheers,

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Nov 20, 2000

Comment author: administrator

Bug in user code

@vicuna vicuna closed this as completed Nov 20, 2000
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant