Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0000221OCamlOCaml generalpublic2000-11-16 15:382000-11-20 12:01
Reporteradministrator 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionno change required 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000221: Change in C-interface?
DescriptionHello,

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 [^]

TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0002179)
administrator (administrator)
2000-11-16 20:04

> 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

(0002180)
administrator (administrator)
2000-11-20 12:01

Bug in user code

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker