FontEditor: Cannot take reference to local lambda

Under the hood, a lambda is just a struct full of pointers/references/copies and whatever else
the compiler deems necessary. In the case of 'update_demo', the struct lives on the stack
frame of FontEditorWidget::FontEditorWidget(). Hence it is still alive when it's called
during the constructor.

However, when 'fixed_width_checkbox.on_checked' fires, that stack frame is no longer alive,
and thus the *reference* to the (struct of) the lambda is invalid\! This meant that
'update_demo' silently read invalid data, tried to call '.update()' on some innocent arbitrary
memory address, and it crashed somewhere unrelated.

Passing 'update_demo' by value (like with all the other event handlers) fixes this issue.
Note that this solution only works because 'update_demo' itself has no state; otherwise
the various copies of 'update_demo' might notice that they are, in fact, independent copies
of the original lambda. But that doesn't matter here.
This commit is contained in:
Ben Wiederhake 2020-08-29 13:09:08 +02:00 committed by Andreas Kling
parent 61521315ed
commit c9bafa9467
Notes: sideshowbarker 2024-07-19 03:00:56 +09:00

View file

@ -263,7 +263,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
info_label.set_text(builder.to_string()); info_label.set_text(builder.to_string());
}; };
fixed_width_checkbox.on_checked = [&](bool checked) { fixed_width_checkbox.on_checked = [&, update_demo](bool checked) {
m_edited_font->set_fixed_width(checked); m_edited_font->set_fixed_width(checked);
glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width()); glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width());
glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph())); glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));