]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/debugging.h
correct a typo in wxSplitterWindow gravity parameter name
[wxWidgets.git] / docs / doxygen / overviews / debugging.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
d54cf7ff 2// Name: debugging.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
880efa2a 9/**
36c9828f 10
928f1a07 11@page overview_debugging Debugging
36c9828f 12
928f1a07 13Classes, functions and macros: wxDebugContext, wxObject, wxLog,
6c75df87 14 @ref group_funcmacro_log, @ref group_funcmacro_debug
d54cf7ff 15
928f1a07
FM
16Various classes, functions and macros are provided in wxWidgets to help you debug
17your application. Most of these are only available if you compile both wxWidgets,
18your application and @e all libraries that use wxWidgets with the __WXDEBUG__ symbol
19defined. You can also test the __WXDEBUG__ symbol in your own applications to execute
20code that should be active only in debug mode.
d54cf7ff 21
928f1a07
FM
22@li @ref overview_debugging_dbgctx
23@li @ref overview_debugging_dbgmacros
24@li @ref overview_debugging_logging
25@li @ref overview_debugging_dbgctx2
30738aae
FM
26
27
928f1a07 28<hr>
d54cf7ff
FM
29
30
928f1a07 31@section overview_debugging_dbgctx wxDebugContext
d54cf7ff 32
928f1a07
FM
33wxDebugContext is a class that never gets instantiated, but ties together
34various static functions and variables. It allows you to dump all objects to that stream,
35write statistics about object allocation, and check memory for errors.
d54cf7ff 36
928f1a07
FM
37It is good practice to define a wxObject::Dump member function for each class you derive
38from a wxWidgets class, so that wxDebugContext::Dump can call it and
39give valuable information about the state of the application.
d54cf7ff 40
928f1a07
FM
41If you have difficulty tracking down a memory leak, recompile
42in debugging mode and call wxDebugContext::Dump and wxDebugContext::PrintStatistics at
43appropriate places. They will tell you what objects have not yet been
44deleted, and what kinds of object they are. In fact, in debug mode wxWidgets will automatically
45detect memory leaks when your application is about to exit, and if there are any leaks,
46will give you information about the problem. (How much information depends on the operating system
47and compiler -- some systems don't allow all memory logging to be enabled). See the
48memcheck sample for example of usage.
d54cf7ff 49
928f1a07
FM
50For wxDebugContext to do its work, the @e new and @e delete operators for wxObject
51have been redefined to store extra information about dynamically allocated objects
52(but not statically declared objects).
d54cf7ff 53
928f1a07
FM
54This slows down a debugging version of an application, but can
55find difficult-to-detect memory leaks (objects are not
56deallocated), overwrites (writing past the end of your object) and
57underwrites (writing to memory in front of the object).
d54cf7ff 58
928f1a07
FM
59If debugging mode is on and the symbols wxUSE_GLOBAL_MEMORY_OPERATORS and
60wxUSE_DEBUG_NEW_ALWAYS are set to 1 in setup.h, 'new' is defined to be:
36c9828f 61
928f1a07
FM
62@code
63#define new new(__FILE__,__LINE__)
64@endcode
36c9828f 65
928f1a07
FM
66All occurrences of 'new' in wxWidgets and your own application will use
67the overridden form of the operator with two extra arguments. This means that
68the debugging output (and error messages reporting memory problems) will tell you what
69file and on what line you allocated the object. Unfortunately not all
70compilers allow this definition to work properly, but most do.
d54cf7ff
FM
71
72
73
928f1a07 74@section overview_debugging_dbgmacros Debug macros
d54cf7ff 75
6c75df87 76You should also use @ref group_funcmacro_debug as part of a 'defensive programming'
928f1a07
FM
77strategy, scattering wxASSERTs liberally to test for problems in your code as early as
78possible.
79Forward thinking will save a surprising amount of time in the long run.
d54cf7ff 80
928f1a07
FM
81#wxASSERT is used to pop up an error message box when a condition
82is not @true. You can also use #wxASSERT_MSG to supply your
83own helpful error message. For example:
36c9828f 84
928f1a07
FM
85@code
86void MyClass::MyFunction(wxObject* object)
87{
88 wxASSERT_MSG( (object != NULL), "object should not be NULL in MyFunction!" );
36c9828f 89
928f1a07
FM
90 ...
91};
92@endcode
36c9828f 93
928f1a07
FM
94The message box allows you to continue execution or abort the program. If you are running
95the application inside a debugger, you will be able to see exactly where the problem was.
36c9828f
FM
96
97
36c9828f 98
928f1a07 99@section overview_debugging_logging Logging functions
d54cf7ff 100
928f1a07
FM
101You can use the wxLogDebug and wxLogTrace functions to output debugging information in
102debug mode; it will do nothing for non-debugging code.
d54cf7ff
FM
103
104
105
928f1a07 106@section overview_debugging_dbgctx2 wxDebugContext overview
d54cf7ff 107
928f1a07 108Class: wxDebugContext
d54cf7ff 109
928f1a07 110wxDebugContext is a class for performing various debugging and memory tracing operations.
d54cf7ff 111
928f1a07
FM
112This class has only static data and function members, and there should be
113no instances. Probably the most useful members are SetFile (for directing output
114to a file, instead of the default standard error or debugger output);
115Dump (for dumping the dynamically allocated objects) and PrintStatistics
116(for dumping information about allocation of objects). You can also call
117Check to check memory blocks for integrity.
d54cf7ff 118
928f1a07
FM
119Here's an example of use. The SetCheckpoint ensures that only the
120allocations done after the checkpoint will be dumped.
36c9828f 121
928f1a07
FM
122@code
123wxDebugContext::SetCheckpoint();
36c9828f 124
928f1a07 125wxDebugContext::SetFile("c:\\temp\\debug.log");
36c9828f 126
928f1a07 127wxString *thing = new wxString;
36c9828f 128
928f1a07 129char *ordinaryNonObject = new char[1000];
36c9828f 130
928f1a07
FM
131wxDebugContext::Dump();
132wxDebugContext::PrintStatistics();
133@endcode
36c9828f 134
928f1a07
FM
135You can use wxDebugContext if __WXDEBUG__ is defined, or you can use it
136at any other time (if wxUSE_DEBUG_CONTEXT is set to 1 in setup.h). It is not disabled
137in non-debug mode because you may not wish to recompile wxWidgets and your entire application
138just to make use of the error logging facility.
36c9828f 139
928f1a07
FM
140@note wxDebugContext::SetFile has a problem at present, so use the default stream instead.
141 Eventually the logging will be done through the wxLog facilities instead.
36c9828f 142
d54cf7ff 143*/
36c9828f 144