这种难以理解的行为

时间:2013-03-26 10:59:26

标签: vala

我在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 而不是构造函数中引发细分错误?

2 个答案:

答案 0 :(得分:0)

最有可能的是,new Main()的结果在构建和垃圾收集后超出了范围。将其分配给变量。

答案 1 :(得分:0)

我更了解这是如何运作的。

在我的项目中,我创建了一个UIHandler类来处理UI事件。

当我在其构造函数中连接此类的实例时,它不起作用,但是如果我在类之外连接实例它就可以正常工作。

错误:这将以分段错误终止。

// Constructor
public UIHandler (Builder builder) {
    var button = builder.get_object ("send-button") as Button;
    Object (button: button);

    bulder.connect_signals (this);
}
...
[CCode (instance_pos = -1)]
public void activate (Entry location) {
    this.button.clicked ();
}

正确:这会让人感到震惊。

主要课程:

public Main ()
{

    try 
    {
        var builder = new Builder ();
        builder.add_from_file (UI_FILE);

        builder.connect_signals (new UIHandler (builder));

        var window = builder.get_object ("window") as Window;

        /* ANJUTA: Widgets initialization for gtk_http_client.ui - DO NOT REMOVE */
        window.show_all ();
    } 
    catch (Error e) {
        stderr.printf ("Could not load UI: %s\n", e.message);
    } 

}

UIHandler类:

// Constructor
public UIHandler (Builder builder) {
    var button = builder.get_object ("send-button") as Button;
    Object (button: button);
}
...
[CCode (instance_pos = -1)]
public void activate (Entry location) {
    this.button.clicked ();
}

这个行为解决了我原来的问题。