Hi, there
Today when I wrote some scripts for my window manager in
OCaml, I ran into the
"Segmentation fault" problem. I have minimized
the problematic code into
several lines as below.
<code: test.ml>
open Unix;;
let dmenu menu_list =
let ic,oc = open_process "dmenu" in
let () =
output_string oc menu_list;
ignore (close_out oc) in
let selected =
try input_line ic with End_of_file -> ""
in
let () = ignore (close_process (ic,oc)) in
selected;;
(* a test loop, the problem will appear after certain rounds
*)
for i = 1 to 300 do
let selected = dmenu
"abcd\nefgh\nijkl\nmnop\n" in
print_endline ((string_of_int i)^selected)
done;;
</code>
The mechanics of this piece of code is exactly the same as
the one described
in two previous threads in the mailing list archive:
[1] http://caml.inria
.fr/pub/ml-archives/caml-list/2002/02/e79999494775664bd1f39c
7bf74b5244.en.html
[2] http://caml.inria
.fr/pub/ml-archives/caml-list/2004/05/e449e178195aabfd25b63f
aef4c1b580.en.html
In short, in such cases, you have to
"open_process" a external command, feed
it the input data, close its input to produce the EOF
**before** I get any
data from its output, at last you close both ends.
The external command is "dmenu", a program
reads a list of newline-separated
items from stdin and creates a menu. When the user selects
an item, his
choice is printed to stdout and dmenu terminates. dmenu is
very small and has a
Debian package, so if you'd like to repeat my test exactly,
it should be
convenient to have it installed.
The phenomenon:
I built the above code in 3 way:
1) ocaml unix.cma test.ml
2) ocamlc -o test_byte unix.cma test.ml
./test_byte
3) ocamlopt -o test_native unix.cmxa test.ml
./test_native
All of them resulted with a "Segmentation fault"
error after certain rounds, no
matter I chose the menu in a normal or fast speed. In my old
PIII500 machine,
the script version *always* produced the error at 44th step;
both the bytecode
and native code versions *always* produced the error at
131th step. A
try...with exception handler won't be able to catch such
low-level error.
Why did this happen? OCaml shouldn't produce segmentation
fault error, should
it? Can it be the problem of dmenu? but it seems that dmenu
existed normally,
otherwise it should be a exception caught by OCaml not a
OCaml crash. I tested
dmenu with a shell script version as below, it worked just
fine.
<code:test.bash>
for i in `seq 1 300`
do
echo -e "abcd\nefgh\nijkl\nmnop\n" | dmenu
echo $i
done
</code>
- code17
Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http:/
/groups.yahoo.com/group/ocaml_beginners/
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|