| 9 | |
| 10 | The windows debugging how to is based on Microsoft Visual Studio 2008 Professional. Using another MSVC Version should be possible without modification. Should... :) |
| 11 | |
| 12 | The standart program debugging regarding software errors is easy: |
| 13 | * On Crashes: Start the software with the debugger, use it until it crashes. The debugger will stop and you can go back at the call stack and look for the first function located in your programm. This function is your first approach to search for your error :) |
| 14 | * On program logic errors without crash: Place debug messages in your code, and set breakpoints at interesting lines so you can watch variable values when the debugger break at that points. |
| 15 | |
| 16 | === Memory Leak Debugging === |
| 17 | While MSVC outputs memoryleaks automatically. it is very hard to modify it's behaviour to get useful leak reports. |
| 18 | |
| 19 | What do wo want? A report of every leak with the following information |
| 20 | * Filename whre the leak was caused. |
| 21 | * Function name where the leak was caused. |
| 22 | * Linenumer where the leak was caused. |
| 23 | |
| 24 | todo: how to modify sourcecode to get the informations. |
| 25 | |
| 26 | pre main() |
| 27 | {{{ |
| 28 | #!cpp |
| 29 | #ifdef _DEBUG |
| 30 | #ifdef WIN32 |
| 31 | // Declare this in header. |
| 32 | #define _CRTDBG_MAP_ALLOC |
| 33 | #include <stdlib.h> |
| 34 | #include <crtdbg.h> |
| 35 | #endif |
| 36 | #endif |
| 37 | }}} |
| 38 | |
| 39 | Inside main() |
| 40 | {{{ |
| 41 | #!cpp |
| 42 | #ifdef _DEBUG |
| 43 | #ifdef WIN32 |
| 44 | #include <leakDetection.h> // for main: must be inside function. In classes: headerfile inside calss is sufficient |
| 45 | int tmp_flag; |
| 46 | |
| 47 | HANDLE log_file = CreateFile("mem_log.txt", GENERIC_WRITE,FILE_SHARE_WRITE, |
| 48 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 49 | |
| 50 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | |
| 51 | _CRTDBG_MODE_DEBUG); |
| 52 | _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); |
| 53 | _CrtSetReportMode(_CRT_ERROR,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | |
| 54 | _CRTDBG_MODE_DEBUG); |
| 55 | |
| 56 | // output to the file |
| 57 | _CrtSetReportFile(_CRT_ASSERT, log_file); |
| 58 | _CrtSetReportFile(_CRT_WARN, log_file); |
| 59 | _CrtSetReportFile(_CRT_ERROR, log_file); |
| 60 | |
| 61 | tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
| 62 | tmp_flag |= _CRTDBG_ALLOC_MEM_DF; |
| 63 | tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF; |
| 64 | tmp_flag |= _CRTDBG_LEAK_CHECK_DF; |
| 65 | |
| 66 | _CrtSetDbgFlag(tmp_flag); |
| 67 | #endif |
| 68 | #endif |
| 69 | }}} |
| 70 | |
| 71 | inside seperate include file: |
| 72 | {{{ |
| 73 | #!cpp |
| 74 | #ifdef _DEBUG |
| 75 | #ifdef WIN32 |
| 76 | #ifndef DBG_NEW |
| 77 | #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) |
| 78 | #define new DBG_NEW |
| 79 | #endif |
| 80 | #endif |
| 81 | #endif |
| 82 | }}} |
| 83 | |
| 84 | This include file has to be included in the header file of each class, because the define redefinition of new is file based. |
| 85 | |
| 86 | todo: output. |
| 87 | |
| 88 | |
| 89 | |
| 90 | todo: false positives |
| 91 | |
| 92 | Set your brakpoints, |