Make a Simple wxWidgets Program Without Memory Leaks
===

I'm trying to make a basic wxWidgets program that doesn't leak any memory (I'm developing on Windows 7 and am using Visual Studio 2010 and trying to use CRT to check for leaks).

I started from the OpenGL sample and gradually worked it down.  After adding CRT calls to the OnExit method of my wxApp object (the *only* place I ever even saw it mentioned), I realized that memory was being leaked everywhere.

I gradually worked it down more until I created this sample code, which makes CRT spit out a huge load of leaks:

    #include <wx/glcanvas.h>
    #include <wx/wxprec.h>
    #ifndef WX_PRECOMP
    	#include <wx/wx.h>
    #endif
    
    #ifdef __WXMSW__
	#include <wx/msw/msvcrt.h>
    #endif
    #if !defined(_INC_CRTDBG)// || !defined(_CRTDBG_MAP_ALLOC)
    	#error "Debug CRT functions have not been included!"
    #endif
    
    class App : public wxApp {
    	public:
    		bool OnInit(void);
    		int OnExit(void);
    };
    bool App::OnInit(void) {
    	if (!wxApp::OnInit()) return false;
    	return true;
    }
    int App::OnExit(void) {
    	return wxApp::OnExit();
    }
    
    int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_prev_instance, wxCmdLineArgType cmd_line, int cmd_show) {
    	int leaks = _CrtDumpMemoryLeaks();
    	if (leaks) {
    		int i=0, j=6/i; //Put a breakpoint here or throw an exception
    	}
    
    	return EXIT_SUCCESS;
    }
    
    #pragma comment(lib,"wxbase29ud.lib")
    #pragma comment(lib,"wxmsw29ud_gl.lib")
    #pragma comment(lib,"wxmsw29ud_core.lib")
    #pragma comment(lib,"wxpngd.lib")
    #pragma comment(lib,"wxzlibd.lib")
    #pragma comment(lib,"comctl32.lib")
    #pragma comment(lib,"rpcrt4.lib")

Notice that the class App is not used anywhere.  The function definitions outside the class are necessary to prevent it being optimized away.  If the class App is _not_ present, then no errors occur.

The questions are, why isn't this working?  How can I make a leak free wxWidgets program?  How should I use _CrtDumpMemoryLeaks()?  Why aren't there resources about this--and if there are, where are they?  The best I could find was [this][1], which only suggested using CRT, but didn't actually say how.  Help?


  [1]: http://wiki.wxwidgets.org/Avoiding_Memory_Leaks