]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdebug.tex
Added Forty Thieves
[wxWidgets.git] / docs / latex / wx / tdebug.tex
CommitLineData
a660d684
KB
1\section{Debugging overview}\label{debuggingoverview}
2
3Classes: \helpref{wxDebugContext}{wxdebugcontext}, \helpref{wxDebugStreamBuf}{wxdebugstreambuf},
4\rtfsp\helpref{wxObject}{wxobject}
5
6Various classes, functions and macros are provided in wxWindows to help you debug
7your application. Most of these are only available if you compile both wxWindows,
8your application and {\it all} libraries that use wxWindows with the DEBUG flag
9set to 1 or more.
10
11wxDebugContext is a class that never gets instantiated, but ties together
12various functions and variables. It allows you to set the debugging stream, dump
13all objects to that stream, write statistics about object allocation, and
14check memory for errors.
15
16You can use the \helpref{WXTRACE}{trace} macro to output debugging information in DEBUG mode;
17it will be defined to nothing for non-debugging code.
18
19It is good practice to define a Dump member function for each class you derive
20from a wxWindows class, so that wxDebugContext::Dump can call it and
21give valuable information about the state of the application.
22
23For wxDebugContext to do its work, the {\it new} and {\it delete}\rtfsp
24operators for wxObject have been redefined to store extra information
25about dynamically allocated objects (but not statically declared
26objects). This slows down a debugging version of an application, but can
27in theory find difficult-to-detect memory leaks (objects are not
28deallocated), overwrites (writing past the end of your object) and
29underwrites (writing to memory in front of the object).
30
31If you have difficulty tracking down a memory leak, recompile
32in debugging mode and call wxDebugContext::Dump and wxDebugContext::Statistics
33at appropriate places. They will tell you what objects have not yet been
34deleted, and what kinds of object they are.
35
36If you use the macro WXDEBUG\_NEW instead of the normal 'new', the debugging
37output (and error messages reporting memory problems) will also tell you what
38file and on what line you allocated the object.
39
40To avoid the need for replacing existing new operators with WXDEBUG\_NEW, you
41can write this at the top of each application file:
42
43\begin{verbatim}
44#define new WXDEBUG\_NEW
45\end{verbatim}
46
47In non-debugging mode, this will revert to the usual interpretation
48of new. Note that for this not to mess up new-based allocation of non-wxObject derived classes and
49built-in types, there are global definitions of new and delete which match
50the syntax required for storing filename and line numbers. These merely
51call malloc and free, and so do not do anything interesting. The definitions
52may possibly cause multiple symbol problems for some compilers and so might
53need to be omitted by setting the USE\_GLOBAL\_MEMORY\_OPERATORS to 0 in wx\_setup.h
54
55\subsection{wxDebugContext overview}\label{wxdebugcontextoverview}
56
57\overview{Debugging overview}{debuggingoverview}
58
59Class: \helpref{wxDebugContext}{wxdebugcontext}
60
61wxDebugContext is a class for performing various debugging and memory tracing
62operations. wxDebugContext, and the related macros and function WXTRACE and
63wxTrace, are only present if USE\_DEBUG\_CONTEXT is used.
64
65This class has only static data and function members, and there should be
66no instances. Probably the most useful members are SetFile (for directing output
67to a file, instead of the default standard error or debugger output);
68Dump (for dumping the dynamically allocated objects) and PrintStatistics
69(for dumping information about allocation of objects). You can also call
70Check to check memory blocks for integrity.
71
72Here's an example of use. The SetCheckpoint ensures that only the
73allocations done after the checkpoint will be dumped. Unfortunately
74the define of new to WXDEBUG\_NEW does not work for Borland C++ (and
75perhaps other compilers) because it fails to find the correct overloaded
76operator for non-object usage of new. Instead, you need to use WXDEBUG\_NEW
77explicitly if there are any examples of non-object new usage in the file.
78
79\begin{verbatim}
80#define new WXDEBUG_NEW
81
82 wxDebugContext::SetCheckpoint();
83
84 wxDebugContext::SetFile("c:\\temp\\debug.log");
85
86 wxString *thing = new wxString;
87
88 // Proves that defining 'new' to be 'WXDEBUG_NEW' doesn't mess up
89 // non-object allocation. Doesn't work for Borland C++.
90 char *ordinaryNonObject = new char[1000];
91
92 wxDebugContext::Dump();
93 wxDebugContext::PrintStatistics();
94\end{verbatim}
95
96You can use wxDebugContext if DEBUG is 1 or more, or you can use it
97at any other time (if USE\_DEBUG\_CONTEXT is 1). It is not disabled
98for DEBUG = 1 (as in earlier versions of wxWindows) because you
99may not wish to recompile wxWindows and your entire application
100just to make use of the error logging facility. This is especially
101true in a Windows NT or Windows 95 environment, where you cannot
102easily output to a debug window: wxDebugContext can be used to
103write to log files instead.
104