2008-02-18 19:04:03 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2008-02-22 17:05:22 -05:00
|
|
|
// Name: debugging.h
|
2008-02-18 19:04:03 -05:00
|
|
|
// Purpose: topic overview
|
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-03-12 04:50:42 -04:00
|
|
|
/**
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@page overview_debugging Debugging
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
Classes, functions and macros: wxDebugContext, wxObject, wxLog,
|
2008-04-06 10:49:48 -04:00
|
|
|
@ref group_funcmacro_log, @ref group_funcmacro_debug
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04: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 @e all libraries that use wxWidgets with the __WXDEBUG__ symbol
|
|
|
|
defined. You can also test the __WXDEBUG__ symbol in your own applications to execute
|
|
|
|
code that should be active only in debug mode.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@li @ref overview_debugging_dbgctx
|
|
|
|
@li @ref overview_debugging_dbgmacros
|
|
|
|
@li @ref overview_debugging_logging
|
|
|
|
@li @ref overview_debugging_dbgctx2
|
2008-03-02 10:33:26 -05:00
|
|
|
|
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
<hr>
|
2008-02-22 17:05:22 -05:00
|
|
|
|
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@section overview_debugging_dbgctx wxDebugContext
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
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.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
It is good practice to define a wxObject::Dump member function for each class you derive
|
|
|
|
from a wxWidgets class, so that wxDebugContext::Dump can call it and
|
|
|
|
give valuable information about the state of the application.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
If you have difficulty tracking down a memory leak, recompile
|
|
|
|
in debugging mode and call wxDebugContext::Dump and wxDebugContext::PrintStatistics at
|
|
|
|
appropriate places. They will tell you what objects have not yet been
|
|
|
|
deleted, and what kinds of object they are. In fact, in debug mode wxWidgets will automatically
|
|
|
|
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.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
For wxDebugContext to do its work, the @e new and @e delete operators for wxObject
|
|
|
|
have been redefined to store extra information about dynamically allocated objects
|
|
|
|
(but not statically declared objects).
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
This slows down a debugging version of an application, but can
|
|
|
|
find difficult-to-detect memory leaks (objects are not
|
|
|
|
deallocated), overwrites (writing past the end of your object) and
|
|
|
|
underwrites (writing to memory in front of the object).
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04: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:
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@code
|
|
|
|
#define new new(__FILE__,__LINE__)
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
All occurrences of 'new' in wxWidgets and your own application will use
|
|
|
|
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.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@section overview_debugging_dbgmacros Debug macros
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-04-06 10:49:48 -04:00
|
|
|
You should also use @ref group_funcmacro_debug as part of a 'defensive programming'
|
2008-03-22 13:49:09 -04:00
|
|
|
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.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
#wxASSERT is used to pop up an error message box when a condition
|
|
|
|
is not @true. You can also use #wxASSERT_MSG to supply your
|
|
|
|
own helpful error message. For example:
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@code
|
|
|
|
void MyClass::MyFunction(wxObject* object)
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( (object != NULL), "object should not be NULL in MyFunction!" );
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
...
|
|
|
|
};
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04: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.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@section overview_debugging_logging Logging functions
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
You can use the wxLogDebug and wxLogTrace functions to output debugging information in
|
|
|
|
debug mode; it will do nothing for non-debugging code.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@section overview_debugging_dbgctx2 wxDebugContext overview
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
Class: wxDebugContext
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
wxDebugContext is a class for performing various debugging and memory tracing operations.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04: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.
|
2008-02-22 17:05:22 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
Here's an example of use. The SetCheckpoint ensures that only the
|
|
|
|
allocations done after the checkpoint will be dumped.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@code
|
|
|
|
wxDebugContext::SetCheckpoint();
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
wxDebugContext::SetFile("c:\\temp\\debug.log");
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
wxString *thing = new wxString;
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
char *ordinaryNonObject = new char[1000];
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
wxDebugContext::Dump();
|
|
|
|
wxDebugContext::PrintStatistics();
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04: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
|
|
|
|
in non-debug mode because you may not wish to recompile wxWidgets and your entire application
|
|
|
|
just to make use of the error logging facility.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-22 13:49:09 -04:00
|
|
|
@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.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-22 17:05:22 -05:00
|
|
|
*/
|
2008-02-19 08:28:24 -05:00
|
|
|
|