memcheck.cpp 3.2 KB

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