执行gtk应用程序时出现段错误

时间:2014-09-11 08:41:11

标签: ocaml

我跟随https://ocaml.org/learn/tutorials/introduction_to_gtk.html 但是当我使用第一个代码和命令时:

ocamlfind ocamlc -g -package lablgtk2 -linkpkg simple.ml -o simple

我收到了警告:

File "simple.ml", line 8, characters 2-44:
Warning 10: this expression should have type unit.
File "simple.ml", line 18, characters 2-54:
Warning 10: this expression should have type unit.
File "simple.ml", line 23, characters 2-69:
Warning 10: this expression should have type unit.

当我命令时:./simple我得到了什么:

(process:7015): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(process:7015): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

(process:7015): Gtk-CRITICAL **: IA__gtk_settings_get_for_screen: assertion 'GDK_IS_SCREEN (screen)' failed

(process:7015): GLib-GObject-CRITICAL **: g_object_get: assertion 'G_IS_OBJECT (object)' failed

(process:7015): Gtk-CRITICAL **: IA__gtk_settings_get_for_screen: assertion 'GDK_IS_SCREEN (screen)' failed

(process:7015): GLib-GObject-CRITICAL **: g_object_get: assertion 'G_IS_OBJECT (object)' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed

(process:7015): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
segfault.

我哪里错了?毕竟,这是来自官方网站的例子。

1 个答案:

答案 0 :(得分:3)

看起来本教程的代码没有调用lablgtk的初始化程序(后者负责初始化gtk的内部结构)。根据{{​​3}},您只需将编译命令行更改为

即可
ocamlfind ocamlc -g -package lablgtk2.auto-init -linkpkg simple.ml -o simple

或者,您可以直接在simple.ml的开头添加初始化代码:

let _ = GtkMain.Main.init ()