// Build with:
// valac TestClosure.vala --pkg gtk+-2.0

using GLib;
using Gtk;

public class TestSource : GLib.Object
{
    public string name {get; set;}
}

public class TestView : Gtk.VBox
{
    public unowned GLib.List<TestSource> sources {get; set;}
    
    public TestView()
    {
        this.notify["sources"].connect(update_sources);
    }
    
    private void update_sources()
    {
        foreach(TestSource src in this.sources)
        {
            Gtk.Button button = new Gtk.Button.with_label(src.name);
            TestSource source = src;
            
            button.clicked.connect(() => {
                stdout.printf("%p\n", this);
                stdout.printf("%p\n", src); // comment out this line and rebuild
                stdout.printf("%p\n", source); // works, instead!
            });
            
            this.pack_start(button, false, false, 0);
        }
    }
}

public class MainClass : GLib.Object
{
    public static int main(string[] args)
    {
        var source = new TestSource();
        source.name = "Test Source";
        
        var list = new GLib.List<TestSource>();
        list.insert(source, 0);
        
        Gtk.init(ref args);
        
        var window = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
        var ssv = new TestView();
        ssv.sources = list;
        
        window.add(ssv);
        
        window.show_all();
        
        Gtk.main();
        
        return 0;
    }
}
