VincentWei / MiniGUI

A modern and mature cross-platform window system for embedded systems and smart IoT devices.
http://www.minigui.com
GNU General Public License v3.0
695 stars 157 forks source link

button destroy caused double free #34

Closed GarrickLin closed 3 years ago

GarrickLin commented 4 years ago

I created an esay encapsulation that each widget will release automatically.

struct WidgetType {
    WidgetType() {}
    WidgetType(const char* spClassName,
        const char* spCaption, DWORD dwStyle, LINT id,
        int x, int y, int w, int h, HWND hParentWnd,
        DWORD dwAddData)
    {
        Create(spClassName, spCaption, dwStyle, id,
            x, y, w, h, hParentWnd, dwAddData);
    }
    ~WidgetType()
    {
        Destroy();
    }
    void Create(const char* spClassName,
        const char* spCaption, DWORD dwStyle, LINT id,
        int x, int y, int w, int h, HWND hParentWnd,
        DWORD dwAddData)
    {
        CHECK_EQ_F(handle, nullptr, "handle is not nullptr");
        handle = CreateWindow(spClassName, spCaption, dwStyle, id,
            x, y, w, h, hParentWnd, dwAddData);
    }
    void Destroy()
    {
           if (handle != nullptr)
               DestroyWindow(handle);
           handle = nullptr;

    }
    bool Empty()
    {
        return handle == nullptr;
    }
    /* data */
    HWND handle = nullptr;
};

but is seems weird that some button caused a double free when I was closing the window:

GUI>Window: MainWindowThreadCleanup called: 0x6cb660 ((null))
GUI>Window: Message queure is freed: 0x6cb660 ((null))
*** Error in `/home/garrick/workspace/cpp/minigui_demo/build/minigui_demo': double free or corruption (!prev): 0x00000000006cd270 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff64f47e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7ffff64fd37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff650153c]
/home/garrick/workspace/cpp/minigui_demo/3rdparty/lib/libminigui_ths-3.2.so.0(+0x5c98c)[0x7ffff798498c]
/home/garrick/workspace/cpp/minigui_demo/3rdparty/lib/libminigui_ths-3.2.so.0(DestroyWindow+0x31)[0x7ffff79d8641]
/home/garrick/workspace/cpp/minigui_demo/build/minigui_demo(_ZN10WidgetType7DestroyEv+0x33)[0x495cd5]
/home/garrick/workspace/cpp/minigui_demo/build/minigui_demo(_ZN10WidgetTypeD1Ev+0x18)[0x495a8c]
/lib/x86_64-linux-gnu/libc.so.6(+0x39ff8)[0x7ffff64b6ff8]
/lib/x86_64-linux-gnu/libc.so.6(+0x3a045)[0x7ffff64b7045]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0x7ffff649d837]
/home/garrick/workspace/cpp/minigui_demo/build/minigui_demo(_start+0x29)[0x45f279]

should I need to destroy the button manually ?

VincentWei commented 4 years ago

MiniGUI will destroy all controls when you destroying a main window. So there is no need to destroy the button manually.