* [Caml-list] Troubles with C->Caml
@ 2001-10-20 23:36 malc
2001-10-21 3:23 ` Daniel de Rauglaudre
0 siblings, 1 reply; 5+ messages in thread
From: malc @ 2001-10-20 23:36 UTC (permalink / raw)
To: caml-list
====
value f (value u)
{
value result;
value res = <some function>;
if (Is_exception_result (res)) {
#ifdef FULL
result = alloc_small (1, 2);
Field (result, 0) = <some value>;
Field (result, 1) = Extract_exception (res);
#else
result = alloc_small (1, 1);
Field (result, 0) = Extract_exception (res);
#endif
return result;
}
result = alloc_small (0, 1);
Field (result, 0) = <some value>;
return result;
}
===
type t
type result = A of t | B of (t * exn)
external cfunc : unit -> result = "f"
let _ =
match cfunc () with
A t -> print_endline "ok"
| B (t, e) -> prerr_endline (Printexc.to_string e)
===
The above dumps core in Printexc.to_string, what am i doing wrong here?
Why if: "type result = A of t | B of exn" and -DFULL everything works
fine?
--
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Troubles with C->Caml
2001-10-20 23:36 [Caml-list] Troubles with C->Caml malc
@ 2001-10-21 3:23 ` Daniel de Rauglaudre
2001-10-21 12:27 ` malc
0 siblings, 1 reply; 5+ messages in thread
From: Daniel de Rauglaudre @ 2001-10-21 3:23 UTC (permalink / raw)
To: caml-list
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.
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.
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Troubles with C->Caml
2001-10-21 3:23 ` Daniel de Rauglaudre
@ 2001-10-21 12:27 ` malc
2001-10-21 13:10 ` Dmitry Bely
2001-11-05 17:56 ` Joerg Czeranski
0 siblings, 2 replies; 5+ messages in thread
From: malc @ 2001-10-21 12:27 UTC (permalink / raw)
To: Daniel de Rauglaudre; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Troubles with C->Caml
2001-10-21 12:27 ` malc
@ 2001-10-21 13:10 ` Dmitry Bely
2001-11-05 17:56 ` Joerg Czeranski
1 sibling, 0 replies; 5+ messages in thread
From: Dmitry Bely @ 2001-10-21 13:10 UTC (permalink / raw)
To: caml-list
malc <malc@pulsesoft.com> writes:
> > > 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.
Why simply not to use CamlIDL and forget about all this problems forever?
Hope to hear from you soon,
Dmitry
-------------------
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Troubles with C->Caml
2001-10-21 12:27 ` malc
2001-10-21 13:10 ` Dmitry Bely
@ 2001-11-05 17:56 ` Joerg Czeranski
1 sibling, 0 replies; 5+ messages in thread
From: Joerg Czeranski @ 2001-11-05 17:56 UTC (permalink / raw)
To: malc; +Cc: caml-list
On Sun, 21 Oct 2001 16:27:08 +0400 (MSD) malc <malc@pulsesoft.com> wrote:
>
> 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.
Hi,
I don't remember whether somebody has answered this,
but I think you should never copy an O'Caml pointer into a
variable that's not in CAMLlocal...(), e.g.
"char *p = String_val (param);" copies the pointer param into
the local variable p. When the GC moves param, p points to
garbage. (Same for h.)
On the other hand you can't declare p with CAMLparam..., so
you probably have to write "String_val (param)" wherever you'd
use p.
I'm not sure whether the documentation mentions this, and
I think it's not completely obvious.
regards,
jörch
-------------------
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2001-11-05 17:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-20 23:36 [Caml-list] Troubles with C->Caml malc
2001-10-21 3:23 ` Daniel de Rauglaudre
2001-10-21 12:27 ` malc
2001-10-21 13:10 ` Dmitry Bely
2001-11-05 17:56 ` Joerg Czeranski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox