]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdebug.tex
define _HPUX_SOURCE under HP-UX, otherwise many things are not defined in standard...
[wxWidgets.git] / docs / latex / wx / tdebug.tex
CommitLineData
a660d684
KB
1\section{Debugging overview}\label{debuggingoverview}
2
6b037754
JS
3Classes, functions and macros: \helpref{wxDebugContext}{wxdebugcontext}, \helpref{wxObject}{wxobject}, \helpref{wxLog}{wxlog},
4\rtfsp\helpref{Log functions}{logfunctions}, \helpref{Debug macros}{debugmacros}
a660d684 5
fc2171bd
JS
6Various classes, functions and macros are provided in wxWidgets to help you debug
7your application. Most of these are only available if you compile both wxWidgets,
8your application and {\it all} libraries that use wxWidgets with the \_\_WXDEBUG\_\_ symbol
6b037754
JS
9defined. You can also test the \_\_WXDEBUG\_\_ symbol in your own applications to execute
10code that should be active only in debug mode.
a660d684 11
6b037754 12\wxheading{wxDebugContext}
a660d684 13
6b037754
JS
14\helpref{wxDebugContext}{wxdebugcontext} is a class that never gets instantiated, but ties together
15various static functions and variables. It allows you to dump all objects to that stream, write statistics about object allocation, and
16check memory for errors.
a660d684 17
6b037754 18It is good practice to define a \helpref{wxObject::Dump}{wxobjectdump} member function for each class you derive
fc2171bd 19from a wxWidgets class, so that \helpref{wxDebugContext::Dump}{wxdebugcontextdump} can call it and
a660d684
KB
20give valuable information about the state of the application.
21
6b037754
JS
22If you have difficulty tracking down a memory leak, recompile
23in debugging mode and call \helpref{wxDebugContext::Dump}{wxdebugcontextdump} and \helpref{wxDebugContext::PrintStatistics}{wxdebugcontextprintstatistics} at
24appropriate places. They will tell you what objects have not yet been
fc2171bd 25deleted, and what kinds of object they are. In fact, in debug mode wxWidgets will automatically
6b037754
JS
26detect memory leaks when your application is about to exit, and if there are any leaks,
27will give you information about the problem. (How much information depends on the operating system
28and compiler -- some systems don't allow all memory logging to be enabled). See the
29memcheck sample for example of usage.
30
a660d684
KB
31For wxDebugContext to do its work, the {\it new} and {\it delete}\rtfsp
32operators for wxObject have been redefined to store extra information
33about dynamically allocated objects (but not statically declared
34objects). This slows down a debugging version of an application, but can
6b037754 35find difficult-to-detect memory leaks (objects are not
a660d684
KB
36deallocated), overwrites (writing past the end of your object) and
37underwrites (writing to memory in front of the object).
38
2fe827d0
JS
39If debugging mode is on and the symbols wxUSE\_GLOBAL\_MEMORY\_OPERATORS and
40wxUSE\_DEBUG\_NEW\_ALWAYS are set to 1 in setup.h, 'new' is defined to be:
6b037754
JS
41
42{\small
43\begin{verbatim}
44#define new new(__FILE__,__LINE__)
45\end{verbatim}
46}%
a660d684 47
fc2171bd 48All occurrences of 'new' in wxWidgets and your own application will use
6b037754
JS
49the overridden form of the operator with two extra arguments. This means that the debugging
50output (and error messages reporting memory problems) will tell you what
51file and on what line you allocated the object. Unfortunately not all
52compilers allow this definition to work properly, but most do.
a660d684 53
6b037754 54\wxheading{Debug macros}
a660d684 55
6b037754
JS
56You should also use \helpref{debug macros}{debugmacros} as part of a `defensive programming' strategy,
57scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
58will save a surprising amount of time in the long run.
59
60\helpref{wxASSERT}{wxassert} is used to pop up an error message box when a condition
61is not true. You can also use \helpref{wxASSERT\_MSG}{wxassertmsg} to supply your
62own helpful error message. For example:
63
64{\small
a660d684 65\begin{verbatim}
6b037754
JS
66 void MyClass::MyFunction(wxObject* object)
67 {
68 wxASSERT_MSG( (object != NULL), "object should not be NULL in MyFunction!" );
69
70 ...
71 };
a660d684 72\end{verbatim}
6b037754
JS
73}
74
75The message box allows you to continue execution or abort the program. If you are running
76the application inside a debugger, you will be able to see exactly where the problem was.
a660d684 77
6b037754
JS
78\wxheading{Logging functions}
79
80You can use the \helpref{wxLogDebug}{wxlogdebug} and \helpref{wxLogTrace}{wxlogtrace} functions to output debugging information in debug mode;
81it will do nothing for non-debugging code.
a660d684
KB
82
83\subsection{wxDebugContext overview}\label{wxdebugcontextoverview}
84
85\overview{Debugging overview}{debuggingoverview}
86
87Class: \helpref{wxDebugContext}{wxdebugcontext}
88
89wxDebugContext is a class for performing various debugging and memory tracing
6b037754 90operations.
a660d684
KB
91
92This class has only static data and function members, and there should be
93no instances. Probably the most useful members are SetFile (for directing output
94to a file, instead of the default standard error or debugger output);
95Dump (for dumping the dynamically allocated objects) and PrintStatistics
96(for dumping information about allocation of objects). You can also call
97Check to check memory blocks for integrity.
98
99Here's an example of use. The SetCheckpoint ensures that only the
6b037754 100allocations done after the checkpoint will be dumped.
a660d684
KB
101
102\begin{verbatim}
a660d684
KB
103 wxDebugContext::SetCheckpoint();
104
105 wxDebugContext::SetFile("c:\\temp\\debug.log");
106
107 wxString *thing = new wxString;
108
a660d684
KB
109 char *ordinaryNonObject = new char[1000];
110
111 wxDebugContext::Dump();
112 wxDebugContext::PrintStatistics();
113\end{verbatim}
114
6b037754
JS
115You can use wxDebugContext if \_\_WXDEBUG\_\_ is defined, or you can use it
116at any other time (if wxUSE\_DEBUG\_CONTEXT is set to 1 in setup.h). It is not disabled
fc2171bd 117in non-debug mode because you may not wish to recompile wxWidgets and your entire application
6b037754
JS
118just to make use of the error logging facility.
119
120Note: wxDebugContext::SetFile has a problem at present, so use the default stream instead.
121Eventually the logging will be done through the wxLog facilities instead.
a660d684 122