]> git.tdb.fi Git - libs/gltk.git/commitdiff
Clear saved input focus if the widget is removed from the container
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Aug 2023 20:25:02 +0000 (23:25 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Aug 2023 21:52:42 +0000 (00:52 +0300)
Checking the saved focus widget's parent in focus_in can lead to use
after free if the widget was deleted in the meantime.

source/container.cpp

index 393973cc2ab7293adcd32c3e939442780e0ff23e..9ef46f1ab85fcd511238ecb83f3f45cf912b7caa 100644 (file)
@@ -35,6 +35,8 @@ void Container::remove(Widget &wdg)
        if(i==children.end())
                throw hierarchy_error("widget not in container");
 
+       if(&wdg==saved_input_focus)
+               saved_input_focus = nullptr;
        wdg.set_parent(nullptr);
        delete *i;
        children.erase(i);
@@ -317,7 +319,7 @@ bool Container::character(wchar_t ch)
 
 void Container::focus_in()
 {
-       if(saved_input_focus && saved_input_focus->get_parent()==this)
+       if(saved_input_focus)
                set_input_focus(saved_input_focus);
        Widget::focus_in();
 }