📄 tdebug.tex
字号:
\section{Debugging overview}\label{debuggingoverview}Classes, functions and macros: \helpref{wxDebugContext}{wxdebugcontext}, \helpref{wxObject}{wxobject}, \helpref{wxLog}{wxlog},\rtfsp\helpref{Log functions}{logfunctions}, \helpref{Debug macros}{debugmacros}Various classes, functions and macros are provided in wxWidgets to help you debugyour application. Most of these are only available if you compile both wxWidgets,your application and {\it all} libraries that use wxWidgets with the \_\_WXDEBUG\_\_ symboldefined. You can also test the \_\_WXDEBUG\_\_ symbol in your own applications to executecode that should be active only in debug mode.\wxheading{wxDebugContext}\helpref{wxDebugContext}{wxdebugcontext} is a class that never gets instantiated, but ties togethervarious static functions and variables. It allows you to dump all objects to that stream, write statistics about object allocation, andcheck memory for errors.It is good practice to define a \helpref{wxObject::Dump}{wxobjectdump} member function for each class you derivefrom a wxWidgets class, so that \helpref{wxDebugContext::Dump}{wxdebugcontextdump} can call it andgive valuable information about the state of the application.If you have difficulty tracking down a memory leak, recompilein debugging mode and call \helpref{wxDebugContext::Dump}{wxdebugcontextdump} and \helpref{wxDebugContext::PrintStatistics}{wxdebugcontextprintstatistics} atappropriate places. They will tell you what objects have not yet beendeleted, and what kinds of object they are. In fact, in debug mode wxWidgets will automaticallydetect 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 systemand compiler -- some systems don't allow all memory logging to be enabled). See thememcheck sample for example of usage.For wxDebugContext to do its work, the {\it new} and {\it delete}\rtfspoperators for wxObject have been redefined to store extra informationabout dynamically allocated objects (but not statically declaredobjects). This slows down a debugging version of an application, but canfind difficult-to-detect memory leaks (objects are notdeallocated), overwrites (writing past the end of your object) andunderwrites (writing to memory in front of the object).If debugging mode is on and the symbols wxUSE\_GLOBAL\_MEMORY\_OPERATORS andwxUSE\_DEBUG\_NEW\_ALWAYS are set to 1 in setup.h, 'new' is defined to be:{\small\begin{verbatim}#define new new(__FILE__,__LINE__)\end{verbatim}}%All occurrences of 'new' in wxWidgets and your own application will usethe overridden form of the operator with two extra arguments. This means that the debuggingoutput (and error messages reporting memory problems) will tell you whatfile and on what line you allocated the object. Unfortunately not allcompilers allow this definition to work properly, but most do.\wxheading{Debug macros}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 thinkingwill save a surprising amount of time in the long run.\helpref{wxASSERT}{wxassert} is used to pop up an error message box when a conditionis not true. You can also use \helpref{wxASSERT\_MSG}{wxassertmsg} to supply yourown helpful error message. For example:{\small\begin{verbatim} void MyClass::MyFunction(wxObject* object) { wxASSERT_MSG( (object != NULL), "object should not be NULL in MyFunction!" ); ... };\end{verbatim}}The message box allows you to continue execution or abort the program. If you are runningthe application inside a debugger, you will be able to see exactly where the problem was.\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.\subsection{wxDebugContext overview}\label{wxdebugcontextoverview}\overview{Debugging overview}{debuggingoverview}Class: \helpref{wxDebugContext}{wxdebugcontext}wxDebugContext is a class for performing various debugging and memory tracingoperations.This class has only static data and function members, and there should beno instances. Probably the most useful members are SetFile (for directing outputto 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 callCheck to check memory blocks for integrity.Here's an example of use. The SetCheckpoint ensures that only theallocations done after the checkpoint will be dumped.\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}You can use wxDebugContext if \_\_WXDEBUG\_\_ is defined, or you can use itat any other time (if wxUSE\_DEBUG\_CONTEXT is set to 1 in setup.h). It is not disabledin non-debug mode because you may not wish to recompile wxWidgets and your entire applicationjust 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -