![]() ![]() ![]() ![]() debugging overviewclasses, functions and macros: wxdebugcontext, wxobject, wxlog, log functions, debug macros 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 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. 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. 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. 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. for wxdebugcontext to do its work, the new and delete 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 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). 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:
debug macros you should also use debug macros 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. 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:
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. logging functions you can use the wxlogdebug and wxlogtrace functions to output debugging information in debug mode; it will do nothing for non-debugging code.
wxdebugcontext overviewclass: wxdebugcontext wxdebugcontext is a class for performing various debugging and memory tracing operations. 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 allocations done after the checkpoint will be dumped.
wxdebugcontext::setcheckpoint(); wxdebugcontext::setfile("c:\\temp\\debug.log"); wxstring *thing = new wxstring; char *ordinarynonobject = new char[1000]; wxdebugcontext::dump(); wxdebugcontext::printstatistics();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. 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.
|