Changes between Version 1 and Version 2 of OsgDebugging


Ignore:
Timestamp:
Jul 31, 2010, 6:58:49 PM (14 years ago)
Author:
Torben Dannhauer
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OsgDebugging

    v1 v2  
    33= Short Introduction to Debugging and Hunting Memory Leaks on Windows and Linux =
    44
     5Debugging the application is quite easy. This indroduction assumes your are familiar with basic debugging techniques (stop and go, watching values..)
     6Because debugging large programs is very slow, debug osgvisual only with small databases. Often, it is even sufficient to debug osgVisual without any terrain database loaded.
     7
    58== Windows ==
     9
     10The windows debugging how to is based on Microsoft Visual Studio 2008 Professional. Using another MSVC Version should be possible without modification. Should... :)
     11
     12The 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 ===
     17While MSVC outputs memoryleaks automatically. it is very hard to modify it's behaviour to get useful leak reports.
     18
     19What 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
     24todo: how to modify sourcecode to get the informations.
     25
     26pre 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
     39Inside 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
     71inside 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
     84This include file has to be included in the header file of each class, because the define redefinition of new is file based.
     85
     86todo: output.
     87
     88
     89
     90todo: false positives
     91
     92Set your brakpoints,
    693
    794== Linux ==
    895
     96{{{
     97valgrind --error-limit=no --leak-check=full ./osgVisuald -m /path/to/database.ive -p yourPath.path
     98}}}