Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does wxWidgets not leak Frames?

Tags:

c++

wxwidgets

I'm trying to learn wxWidgets, but I'm stuck on a point which I can't find an explanation for anywhere in the documentation. I am trying to understand this minimal wxWidgets program:

#include <wx/wx.h>

class MyApp : public wxApp
{
    virtual bool OnInit();
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    wxFrame *frame = new wxFrame(NULL, -1, _("Hello World"), wxPoint(50, 50),
                                  wxSize(450, 350));       
    frame->Show(true);
    return true;
}

Specifically, why is it that frame does not leak? When does it get released and whose responsibility is is? In a normal program a pointer that does not get passed to anything and that goes out of scope without being deleted is almost certainly a leak, but apparently this is not so in wxWidgets.

like image 270
Mankarse Avatar asked Jun 09 '11 06:06

Mankarse


2 Answers

Cleanup is discussed here: http://docs.wxwidgets.org/2.9.2/overview_windowdeletion.html

like image 162
Arafangion Avatar answered Sep 19 '22 17:09

Arafangion


See the note in the Hello World example on the wxWidgets wiki:

http://wiki.wxwidgets.org/Hello_World

"You could be wondering why the frame variable isn't deleted anywhere. By setting the frame as the top window of the application, the application will delete the frame for us (for a more in-depth explanation, see Avoiding Memory Leaks)."

However, the code you posted doesn't call SetTopWindow() the way the code from the wiki does. So I imagine it would leak.

like image 39
HostileFork says dont trust SE Avatar answered Sep 18 '22 17:09

HostileFork says dont trust SE