ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.64k stars 399 forks source link

Crash on exit #115

Closed MichaelGoulding closed 3 years ago

MichaelGoulding commented 3 years ago

Often, when exiting from main, I get this crash:

AdminConsole.exe!std::_Iterator_base12::_Adopt_unlocked(std::_Container_proxy _Parent_proxy=0xdddddddddddddddd) Line 1190 C++ Symbols loaded. AdminConsole.exe!std::_Iterator_base12::_Adopt_locked(std::_Container_proxy _Parent_proxy=0xdddddddddddddddd) Line 1198 C++ Symbols loaded. AdminConsole.exe!std::_Iterator_base12::_Adopt(const std::_Container_base12 _Parent=0x000002f48d016108) Line 1147 C++ Symbols loaded. AdminConsole.exe!std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::shared_ptr>>>::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::shared_ptr>>>(std::shared_ptr _Parg=0xdddddddddddddddd, const std::_Container_base12 _Pvector=0x000002f48d016108) Line 44 C++ Symbols loaded. AdminConsole.exe!std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::shared_ptr>>>::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::shared_ptr>>>(std::shared_ptr ) C++ Symbols loaded. AdminConsole.exe!std::vector<std::shared_ptr,std::allocator<std::shared_ptr>>::end() Line 1486 C++ Symbols loaded. AdminConsole.exe!std::end<std::vector<std::shared_ptr,std::allocator<std::shared_ptr>>>(std::vector<std::shared_ptr,std::allocator<std::shared_ptr>> & _Cont={...}) Line 1927 C++ Symbols loaded. AdminConsole.exe!ftxui::ComponentBase::Detach() Line 139 C++ Symbols loaded. AdminConsole.exe!ftxui::ComponentBase::~ComponentBase() Line 23 C++ Symbols loaded. AdminConsole.exe!ftxui::ContainerBase::~ContainerBase() Line 23 C++ Symbols loaded. AdminConsole.exe!ftxui::ContainerBase::`scalar deleting destructor'(unsigned int) C++ Symbols loaded. AdminConsole.exe!std::_Destroy_in_place(ftxui::ContainerBase & _Obj={...}) Line 313 C++ Symbols loaded. AdminConsole.exe!std::_Ref_count_obj2::_Destroy() Line 2048 C++ Symbols loaded. AdminConsole.exe!std::_Ref_count_base::_Decref() Line 1110 C++ Symbols loaded. AdminConsole.exe!std::_Ptr_base::_Decref() Line 1341 C++ Symbols loaded. AdminConsole.exe!std::shared_ptr::~shared_ptr() Line 1629 C++ Symbols loaded.

ArthurSonzogni commented 3 years ago

Thanks for reporting this issue! Could you help me figure out how to reproduce this?

ArthurSonzogni commented 3 years ago

Are you making one component to contain itself as a children?

MichaelGoulding commented 3 years ago

Here is the repro.

With x64-Debug preset, typing "exit" and pressing enter results in the crash.

ArthurSonzogni commented 3 years ago

Thanks! I will take a look.

ArthurSonzogni commented 3 years ago

I tried: asciicast

but things seems to work.

Maybe this is Windows only. In this case, there are some chances this is fixe by: https://github.com/ArthurSonzogni/FTXUI/commit/18d1b04b7adb1c85515b7663a81d3ca18f255d23

ArthurSonzogni commented 3 years ago

Hey @MichaelGoulding, I landed a few patches. Since I can't reproduce your problem, it is quite hard for me to fix. However I have good hope it might be fixed (~10% probability). Could you please check whether this has been fixed on the latest patch?

MichaelGoulding commented 3 years ago

With this example:

{
    auto cmdInput = Input(&g_currentCmdString, "enter command");

    auto layout = Container::Vertical({
        cmdInput,
        });
}

The layout class is destroyed before the cmdInput class, so the parent pointer points to bogus memory in cmdInput. cmdInput's Detach() method then tries to remove itself from the parent's children list in its destructor, leading to a crash in Window's Debug mode.

MSVC catches this in Debug mode because the memory for layout appears to be set to a known bad value after destruction.

You might be able to reproduce this with AddressSanitizer on Linux.

I think the fix might be to use a weak_ptr for the parent object so that you know if the parent is already destroyed, or you can have the parent component detach all its children on destruction.

ArthurSonzogni commented 3 years ago

Indeed! This is very weird this got unnoticed for so long!

Could you confirm this has been fixed by latest patch?

MichaelGoulding commented 3 years ago

See my comments for #117

ArthurSonzogni commented 3 years ago

See my comments for #117

Fixed! Thanks!

Do you see any other issues?

MichaelGoulding commented 3 years ago

not right now. thanks!