App initialization and cleanup confusion

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

App initialization and cleanup confusion

Post by denarced » Wed Jan 04, 2012 6:40 am

Hello,

I've been trying to create a wxWidgets program which is easy to test; it has a separate main for unit tests and the GUI is dumb. Because of this I couldn't use the normal IMPLEMENT_APP.

Visual Studio warns me about memory leaks and while I have been googling for an hour,
I haven't found a clear cut tutorial on how to initialize and cleanup wxWidgets manually.

Primary main:

Code: Select all

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    View *app = new ConcreteView(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
    Controller controller(app);
    wxTheApp->CallOnInit();
    wxTheApp->OnRun();
    delete app;

    return 0;
}
ConcreteView in turn initializes and cleans up the wxWidgets systems:

Code: Select all

ConcreteView::ConcreteView(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd):
app(new MyApp())
{
    wxEntryStart(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
}

ConcreteView::~ConcreteView()
{
    wxEntryCleanup();
}
ConcreteView also contains a MyApp instance which of course is wxApp

MyApp.h and MyApp.cpp:

Code: Select all

class MyApp : public wxApp
{
public:
    MyApp();
    ~MyApp();

    virtual bool OnInit();
};

MyApp::MyApp()
{
}

MyApp::~MyApp()
{
}

bool MyApp::OnInit()
{
    wxFrame *frame = new wxFrame(NULL, wxID_ANY, "title", wxDefaultPosition, wxSize(250, 150));
    frame->Show(true);
    return true;
}
The deeper purpose for this design is that there is also another class inherited from View, DummyView.
DummyView is used in unit testing main and the ConcreteView in the other main (the actual program).
The purpose is to use create an application where the View is very dumb and the Controller takes care
of nearly everything. This would enable me to just test the Controller while the bugs in the GUI will remain
small in number. So while ConcreteView creates the wxWidgets app, DummyView does nothing (probably
guessed that one :D). And naturally the unit test main doesn't call wxWidgets initialization functions.

So what do you pros think, good enough?
Or wondering that it's not crashing already :)

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: App initialization and cleanup confusion

Post by doublemax » Wed Jan 04, 2012 5:23 pm

AFAIK the whole process of initializing wxWidgets manually is not properly documented anywhere. The best sources of information are the samples "console", "dll" and "mfc".

I think you should have tried with the IMPLEMENT_APP_NO_MAIN macro.

E.g in your code it's unclear to me how the global application pointer wxTheApp gets set. But if the code doesn't crash, obviously someone must have done it ;)

For more background information you could try the wx-users mailing list and ask the wxWidgets developers themselves.
Use the source, Luke!

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: App initialization and cleanup confusion

Post by denarced » Sat Jan 07, 2012 9:13 am

Thank you doublemax.
I have now tried the IMPLEMENT_APP_NO_MAIN macro, things are working smoothly.
Thanks for the tip.

Post Reply