我在处理 vala 类中的这种行为时几乎没有问题。
这是我的代码:(build_and_send1 和 build_and_send2 是按下按钮发出的信号)
using GLib;
using Gtk;
public class Main : Object
{
/*
* Uncomment this line when you are done testing and building a tarball
* or installing
*/
//const string UI_FILE = Config.PACKAGE_DATA_DIR + "/" + "gtk_httpclient.ui";
const string UI_FILE = "src/gtk_httpclient.ui";
/* ANJUTA: Widgets declaration for gtk_httpclient.ui - DO NOT REMOVE */
private Builder builder;
public Main ()
{
try
{
this.builder = new Builder ();
this.builder.add_from_file (UI_FILE);
this.builder.connect_signals (this);
var window = this.builder.get_object ("window") as Window;
/* ANJUTA: Widgets initialization for gtk_httpclient.ui - DO NOT REMOVE */
window.show_all ();
stderr.printf ("constructor:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
catch (Error e) {
stderr.printf ("Could not load UI: %s\n", e.message);
}
}
[CCode (instance_pos = -1)]
public void on_destroy (Widget window)
{
Gtk.main_quit();
}
public void build_and_send1 (Widget button)
{
stderr.printf ("\nbuild_and_send1:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
[CCode (instance_pos = -1)]
public void build_and_send2 (Widget button)
{
stderr.printf ("\nbuild_and_send2:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
static int main (string[] args)
{
Gtk.init (ref args);
new Main ();
Gtk.main ();
return 0;
}
}
我有这个输出:
constructor:
this: 0x1a524a0
builder: 0x1a6a230
build_and_send1:
this: 0x1aa2030
builder: 0x5a4fe823
build_and_send2:
this: 0x1a524a0
Program has been terminated receiving signal 11 (Segmentation fault)
我假设如果我想在我的信号中使用相同的 Main 实例,它们必须以 [CCode (instance_pos = -1)] 开头。但是builder.connect_signals (this)的目标是什么??
为什么 this.builder 在build_and_send2而不是在构造函数中引发分段错误?