]> git.saurik.com Git - wxWidgets.git/blob - samples/memcheck/memcheck.cpp
make some reference handling a little more direct and less obscure
[wxWidgets.git] / samples / memcheck / memcheck.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: memcheck.cpp
3 // Purpose: Memory-checking sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/datetime.h"
24
25 #ifndef wxHAS_IMAGES_IN_RESOURCES
26 #include "../sample.xpm"
27 #endif
28
29 #ifndef __WXDEBUG__
30 #error This program must be compiled in debug mode.
31 #endif
32
33 // Normally, new is automatically defined to be the
34 // debugging version. If not, this does it.
35 #if !defined(new) && defined(WXDEBUG_NEW) && wxUSE_MEMORY_TRACING && wxUSE_GLOBAL_MEMORY_OPERATORS
36 #define new WXDEBUG_NEW
37 #endif
38
39 // Define a new application type
40 class MyApp: public wxApp
41 { public:
42 bool OnInit(void);
43 };
44
45 // Define a new frame type
46 class MyFrame: public wxFrame
47 { public:
48 MyFrame(wxFrame *parent);
49 void OnQuit(wxCommandEvent& event);
50
51 DECLARE_EVENT_TABLE()
52 };
53
54 IMPLEMENT_APP(MyApp)
55
56 // `Main program' equivalent, creating windows and returning main app frame
57 bool MyApp::OnInit(void)
58 {
59 if ( !wxApp::OnInit() )
60 return false;
61
62 // Create the main frame window
63 MyFrame *frame = new MyFrame((wxFrame *) NULL);
64
65 // Give it an icon
66 frame->SetIcon(wxICON(sample));
67
68 // Make a menubar
69 wxMenu *file_menu = new wxMenu;
70
71 file_menu->Append(wxID_EXIT, wxT("E&xit"));
72 wxMenuBar *menu_bar = new wxMenuBar;
73 menu_bar->Append(file_menu, wxT("File"));
74 frame->SetMenuBar(menu_bar);
75
76 // Make a panel with a message
77 wxPanel *panel = new wxPanel(frame);
78
79 (void)new wxStaticText(panel, wxID_ANY, wxT("Hello, this is a minimal debugging wxWidgets program!"), wxPoint(10, 10));
80
81 // Show the frame
82 frame->Show(true);
83
84 #if wxUSE_MEMORY_TRACING
85 wxDebugContext::SetCheckpoint();
86 #endif
87
88 // object allocation
89 wxBrush* brush = new wxBrush(*wxRED_BRUSH);
90 wxBitmap* bitmap = new wxBitmap(100, 100);
91
92 // non-object allocation
93 char *ordinaryNonObject = new char[1000];
94
95 wxString *thing = new wxString;
96
97 #if wxUSE_DATETIME
98 wxDateTime* date = new wxDateTime;
99 #endif // wxUSE_DATETIME
100
101 const char *data = (const char*) thing ;
102
103 #if wxUSE_MEMORY_TRACING
104 // On MSW, Dump() crashes if using wxLogGui,
105 // so use wxLogStderr instead.
106 wxLog* oldLog = wxLog::SetActiveTarget(new wxLogStderr);
107
108 wxDebugContext::PrintClasses();
109 wxDebugContext::Dump();
110 wxDebugContext::PrintStatistics();
111
112 // Set back to wxLogGui
113 delete wxLog::SetActiveTarget(oldLog);
114 #endif
115
116 // Don't delete these objects, to force wxApp to flag a memory leak.
117 // delete thing;
118 // delete date;
119 // delete[] ordinaryNonObject;
120
121 return true;
122 }
123
124 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
125 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
126 END_EVENT_TABLE()
127
128 // My frame constructor
129 MyFrame::MyFrame(wxFrame *parent):
130 wxFrame(parent, wxID_ANY, wxT("MemCheck wxWidgets Sample"), wxDefaultPosition, wxSize(400, 200))
131 {}
132
133 // Intercept menu commands
134 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
135 {
136 Close(true);
137 }
138