wiki:OsgDebugging

Version 3 (modified by Torben Dannhauer, 14 years ago) (diff)

--

Short Introduction to Debugging and Hunting Memory Leaks on Windows and Linux

Debugging the application is quite easy. This indroduction assumes your are familiar with basic debugging techniques (stop and go, watching values..) Because 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.

Windows

The windows debugging how to is based on Microsoft Visual Studio 2008 Professional. Using another MSVC Version should be possible without modification. Should... :)

The standart program debugging regarding software errors is easy:

  • 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 :)
  • 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.

Memory Leak Debugging

While MSVC outputs memoryleaks automatically. it is very hard to modify it's behaviour to get useful leak reports.

What do wo want? A report of every leak with the following information

  • Filename whre the leak was caused.
  • Function name where the leak was caused.
  • Linenumer where the leak was caused.

todo: how to modify sourcecode to get the informations.

pre main()

#ifdef _DEBUG
        #ifdef WIN32
                // Declare this in header.
                #define _CRTDBG_MAP_ALLOC
                #include <stdlib.h>
                #include <crtdbg.h>
        #endif 
#endif 

Inside main()

#ifdef _DEBUG
        #ifdef WIN32
                #include <leakDetection.h>      // for main: must be inside function. In classes: headerfile inside calss is sufficient
                int tmp_flag;

                HANDLE log_file = CreateFile("mem_log.txt", GENERIC_WRITE,FILE_SHARE_WRITE,
                        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

                _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW |
                        _CRTDBG_MODE_DEBUG);
                _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
                _CrtSetReportMode(_CRT_ERROR,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW |
                        _CRTDBG_MODE_DEBUG);

                // output to the file
                _CrtSetReportFile(_CRT_ASSERT, log_file);
                _CrtSetReportFile(_CRT_WARN, log_file);
                _CrtSetReportFile(_CRT_ERROR, log_file);

                tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
                tmp_flag |= _CRTDBG_ALLOC_MEM_DF;
                tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
                tmp_flag |= _CRTDBG_LEAK_CHECK_DF;

                _CrtSetDbgFlag(tmp_flag);
        #endif 
#endif

inside seperate include file:

#ifdef _DEBUG
        #ifdef WIN32
                #ifndef DBG_NEW
                        #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
                        #define new DBG_NEW
                #endif
        #endif
#endif

This include file has to be included in the header file of each class, because the define redefinition of new is file based.

todo: output.

todo: false positives

Set your brakpoints,

Linux

valgrind --error-limit=no --leak-check=full ./osgVisuald -m /path/to/database.ive -p yourPath.path