I don't see any obvious memory leaks in that code, but i'm surprised it doesn't crash.
What's the base class for MainWindow and LoginWindow?
In the following i'll assume it's wxFrame.
Code: Select all
wxGetApp().mainWindow->Close(true);
wxGetApp().mainWindow->Destroy();
delete wxGetApp().mainWindow;
The default behavior of a wxFrame is to destroy itself when it's closed. So this code should normally crash, as mainWindow gets destroyed twice. Usually just calling Close() should be enough. Unless it has a close event handler which prevents the automatic destruction.
Code: Select all
wxGetApp().loginWindow = new LoginWindow(nullptr);
wxGetApp().loginWindow->Show();
I'm not 100% sure whether all toplevel windows get automatically destroyed if the app terminates, but if not and loginWindow is not NULL, this will leak memory as the old instance will not get destroyed.
You should also ask yourself if it's really needed to destroy mainWindow and loginWindow, or if it's not enough to just create them once and then use Hide() and Show().
Almost all the objects I use are derived from wx, like wxString, wxImage etc, which should be deleted automatically, right?
Not necessarily. Generally you don't have to manually destroy any wxWindow, wxSizer and wxMenu, as they will be destroyed by their respective owner or they destroy themselves (wxFrame). (There are a few exceptions).
If if you create any other object like wxString, wxImage etc. on the heap (= with new()) you'll usually have to delete it yourself.