| Attached Files | warning_exception.patch [^] (1,205 bytes) 2012-05-03 11:18 [Show Content] [Hide Content]Index: manual/cmds/intf-c.etex
===================================================================
--- manual/cmds/intf-c.etex (revision 12413)
+++ manual/cmds/intf-c.etex (working copy)
@@ -1273,6 +1273,28 @@
an exception escaped, and its value (the exception descriptor) can be
recovered using "Extract_exception("\var{v}")".
+\paragraph{Warning:} If the OCaml function returned with an exception,
+"Extract_exception" should be applied to the exception result prior
+to calling a function that may trigger garbage collection.
+Otherwise, if \var{v} is reachable during garbage collection, the runtime
+can crash since \var{v} does not contain a valid value.
+
+Example:
+\begin{verbatim}
+ value foo(value vf,value vx)
+ {
+ CAMLparam2(vf,vx);
+ CAMLlocal2(res,tmp);
+ res = caml_callback_exn(vf,vx);
+ if(Is_exception_result(res)) {
+ res = Extract_exception(res);
+ tmp = caml_alloc(3,0); /* Safe to allocate: res contains valid value. */
+ ...
+ }
+ CAMLreturn (res);
+ }
+\end{verbatim}
+
\subsection{Obtaining or registering OCaml closures for use in C functions}
There are two ways to obtain OCaml function values (closures) to
|