From c9bafa9467e2101cf49b0711d0bf13b6a1d8bd86 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 29 Aug 2020 13:09:08 +0200 Subject: [PATCH] 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. --- Applications/FontEditor/FontEditor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Applications/FontEditor/FontEditor.cpp b/Applications/FontEditor/FontEditor.cpp index a7d32a47065..0d8e68340dd 100644 --- a/Applications/FontEditor/FontEditor.cpp +++ b/Applications/FontEditor/FontEditor.cpp @@ -263,7 +263,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr&& edite 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); 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()));