1998-05-20 14:25:30 +00:00
\section { Debugging overview} \label { debuggingoverview}
1998-11-21 15:40:35 +00:00
Classes, functions and macros: \helpref { wxDebugContext} { wxdebugcontext} , \helpref { wxObject} { wxobject} , \helpref { wxLog} { wxlog} ,
\rtfsp \helpref { Log functions} { logfunctions} , \helpref { Debug macros} { debugmacros}
1998-05-20 14:25:30 +00:00
2004-05-04 08:27:20 +00:00
Various classes, functions and macros are provided in wxWidgets to help you debug
your application. Most of these are only available if you compile both wxWidgets,
your application and { \it all} libraries that use wxWidgets with the \_ \_ WXDEBUG\_ \_ symbol
1998-11-21 15:40:35 +00:00
defined. You can also test the \_ \_ WXDEBUG\_ \_ symbol in your own applications to execute
code that should be active only in debug mode.
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
\wxheading { wxDebugContext}
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
\helpref { wxDebugContext} { wxdebugcontext} is a class that never gets instantiated, but ties together
various static functions and variables. It allows you to dump all objects to that stream, write statistics about object allocation, and
check memory for errors.
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
It is good practice to define a \helpref { wxObject::Dump} { wxobjectdump} member function for each class you derive
2004-05-04 08:27:20 +00:00
from a wxWidgets class, so that \helpref { wxDebugContext::Dump} { wxdebugcontextdump} can call it and
1998-05-20 14:25:30 +00:00
give valuable information about the state of the application.
1998-11-21 15:40:35 +00:00
If you have difficulty tracking down a memory leak, recompile
in debugging mode and call \helpref { wxDebugContext::Dump} { wxdebugcontextdump} and \helpref { wxDebugContext::PrintStatistics} { wxdebugcontextprintstatistics} at
appropriate places. They will tell you what objects have not yet been
2004-05-04 08:27:20 +00:00
deleted, and what kinds of object they are. In fact, in debug mode wxWidgets will automatically
1998-11-21 15:40:35 +00:00
detect memory leaks when your application is about to exit, and if there are any leaks,
will give you information about the problem. (How much information depends on the operating system
and compiler -- some systems don't allow all memory logging to be enabled). See the
memcheck sample for example of usage.
1998-05-20 14:25:30 +00:00
For wxDebugContext to do its work, the { \it new} and { \it delete} \rtfsp
operators for wxObject have been redefined to store extra information
about dynamically allocated objects (but not statically declared
objects). This slows down a debugging version of an application, but can
1998-11-21 15:40:35 +00:00
find difficult-to-detect memory leaks (objects are not
1998-05-20 14:25:30 +00:00
deallocated), overwrites (writing past the end of your object) and
underwrites (writing to memory in front of the object).
2004-02-18 11:44:11 +00:00
If debugging mode is on and the symbols wxUSE\_ GLOBAL\_ MEMORY\_ OPERATORS and
wxUSE\_ DEBUG\_ NEW\_ ALWAYS are set to 1 in setup.h, 'new' is defined to be:
1998-11-21 15:40:35 +00:00
{ \small
\begin { verbatim}
#define new new(_ _ FILE_ _ ,_ _ LINE_ _ )
\end { verbatim}
} %
1998-05-20 14:25:30 +00:00
2004-05-04 08:27:20 +00:00
All occurrences of 'new' in wxWidgets and your own application will use
1998-11-21 15:40:35 +00:00
the overridden form of the operator with two extra arguments. This means that the debugging
output (and error messages reporting memory problems) will tell you what
file and on what line you allocated the object. Unfortunately not all
compilers allow this definition to work properly, but most do.
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
\wxheading { Debug macros}
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
You should also use \helpref { debug macros} { debugmacros} as part of a `defensive programming' strategy,
scattering wxASSERTs liberally to test for problems in your code as early as possible. Forward thinking
will save a surprising amount of time in the long run.
\helpref { wxASSERT} { wxassert} is used to pop up an error message box when a condition
is not true. You can also use \helpref { wxASSERT\_ MSG} { wxassertmsg} to supply your
own helpful error message. For example:
{ \small
1998-05-20 14:25:30 +00:00
\begin { verbatim}
1998-11-21 15:40:35 +00:00
void MyClass::MyFunction(wxObject* object)
{
wxASSERT_ MSG( (object != NULL), "object should not be NULL in MyFunction!" );
...
} ;
1998-05-20 14:25:30 +00:00
\end { verbatim}
1998-11-21 15:40:35 +00:00
}
The message box allows you to continue execution or abort the program. If you are running
the application inside a debugger, you will be able to see exactly where the problem was.
1998-05-20 14:25:30 +00:00
1998-11-21 15:40:35 +00:00
\wxheading { Logging functions}
You can use the \helpref { wxLogDebug} { wxlogdebug} and \helpref { wxLogTrace} { wxlogtrace} functions to output debugging information in debug mode;
it will do nothing for non-debugging code.
1998-05-20 14:25:30 +00:00
\subsection { wxDebugContext overview} \label { wxdebugcontextoverview}
\overview { Debugging overview} { debuggingoverview}
Class: \helpref { wxDebugContext} { wxdebugcontext}
wxDebugContext is a class for performing various debugging and memory tracing
1998-11-21 15:40:35 +00:00
operations.
1998-05-20 14:25:30 +00:00
This class has only static data and function members, and there should be
no instances. Probably the most useful members are SetFile (for directing output
to a file, instead of the default standard error or debugger output);
Dump (for dumping the dynamically allocated objects) and PrintStatistics
(for dumping information about allocation of objects). You can also call
Check to check memory blocks for integrity.
Here's an example of use. The SetCheckpoint ensures that only the
1998-11-21 15:40:35 +00:00
allocations done after the checkpoint will be dumped.
1998-05-20 14:25:30 +00:00
\begin { verbatim}
wxDebugContext::SetCheckpoint();
wxDebugContext::SetFile("c:\\ temp\\ debug.log");
wxString *thing = new wxString;
char *ordinaryNonObject = new char[1000];
wxDebugContext::Dump();
wxDebugContext::PrintStatistics();
\end { verbatim}
1998-11-21 15:40:35 +00:00
You can use wxDebugContext if \_ \_ WXDEBUG\_ \_ is defined, or you can use it
at any other time (if wxUSE\_ DEBUG\_ CONTEXT is set to 1 in setup.h). It is not disabled
2004-05-04 08:27:20 +00:00
in non-debug mode because you may not wish to recompile wxWidgets and your entire application
1998-11-21 15:40:35 +00:00
just to make use of the error logging facility.
Note: wxDebugContext::SetFile has a problem at present, so use the default stream instead.
Eventually the logging will be done through the wxLog facilities instead.
1998-05-20 14:25:30 +00:00