Lambda freed prematurely when passed as argument?
I'm not quite sure if it's me doing something wrong or if valac is at fault. I'm using Vala 0.50.1.47-26361 from org.gnome.Sdk//master
.
I'm using GTK 4's SignalListItemFactory
to make a label and set its contents on bind:
delegate void Bind<Widget> (Widget widget, TASLogReader.Row row);
private Bind<Gtk.Label> label_text_from_fn (Format format) {
return (label, row) => {
label.set_label (format (row));
};
}
class TASLogReader.Table {
[GtkChild]
private Gtk.ColumnViewColumn column_frame;
private string? format_frame (Row row) {
return row.number.to_string ();
}
construct {
var factory = new Gtk.SignalListItemFactory ();
factory.setup.connect(list_item => {
list_item.set_child (make_label ());
});
var bind = label_text_from_fn (format_frame);
factory.bind.connect(list_item => {
var widget = (Gtk.Label) list_item.get_child ();
var item = (TASLogReader.Row) list_item.get_item ();
bind (widget, item);
});
column_frame.set_factory (factory);
}
}
All well and good, however if I abstract this into a function:
private void connect_bind (Gtk.SignalListItemFactory factory, Bind<Gtk.Label> bind) {
factory.bind.connect(list_item => {
var widget = (Gtk.Label) list_item.get_child ();
var item = (TASLogReader.Row) list_item.get_item ();
bind (widget, item);
});
}
var bind = label_text_from_fn (format_frame);
connect_bind (factory, bind);
The lambda seems to be freed at the end of construct {}
(while still being used as the bind signal handler), leading to a use-after-free crash.
Edited by Ivan Molodetskikh