Hi Torben,

You can turn on automatic generation of memory leak report, which will
trigger after all deinitialization:

#ifdef _DEBUG

    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 if not under VS
    _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

Linking with MFC is really strange idea in this case.
Also you can use _CrtSetBreakAlloc() function to break on the memory
allocation with specified
number (you can find this number near each allocation in the output).

Best regards,
Yurii Monakov

2009/7/31 Torben Dannhauer <[email protected]>

> Hi,
>
> My Workaround bases also on MFC, but seems to smaller:
>
> My standart project started as an empty console application without MFC.
>
> If I enabled only the memory leak detection, i get lots of memory leak
> notification because OSGS static objects are not clean until the end of the
> applications leak detection.
>
>
> My solution is:
> 1. enable the memory leak detection in the main function:
>
> Code:
>
>  int main(int argc, char** argv)
> {
>   /*// Redirect memory debugging output to console
>   _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
>   _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
>   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
>   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
>   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
>   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
>   */
>
>   //start memory observation
>    _CrtMemState s1;
>    _CrtMemCheckpoint( &s1 );
>
>    {   // this scope is important!! It'ns nessecary to use OSG in a
> subscope of the mainfunction to get no false memory leak indications,
>
>        // use an ArgumentParser object to manage the program arguments.
>                osg::ArgumentParser arguments(&argc,argv);
>
>            // instantiate the applications main class
>                osg::ref_ptr<app_core> core = new app_core(arguments);
>
>            // init app
>            core->initialize();
>    }
>
>        // check for leaks
>   _CrtMemDumpAllObjectsSince( &s1 );
>
>        return 0;
> }
>
>
>
>
> 2. To disable all false notification based on the static variable problem
> (e.g. singletons):
> Edit the MSVC Project:
> ->project settings: Use of the MFC library: change from "USE WINDOWS
> STANDARD LIBRARY" to "USE STATIC MFC LIBRARY"
> (The use of the dynamic MFC dll doesn't work.)
>
> In my project, now all false positives disapper and only the (test) memory
> leak remains.
>
> Unfortunately memory leaks are not related to codefiles and codefiles, so
> fixing is quite problematic. To solve this issue:
>
> 3. Enable Codefile and Codeline identification
>
> Add a file "memoryLeakDetection.h" or something like this to your project.
>
> this File contains:
>
> Code:
>
> #pragma once
>
> #ifdef _DEBUG
>        #include <crtdbg.h>
>        #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)
>        #define new DEBUG_NEW
> #else
>        #define DEBUG_NEW new
> #endif
>
>
>
>
> Incude this file in every .cpp file after your corresponding header File.
>
> I included the file in the .cpp file to get rid of problems resulting from
> multiple direct and indirect inclusion in the header file (pragma once
> doesn't seem to work well in all situations.).
>
> Now you should have only named, numbered and true memory leaks in your
> application :) I hope this spares someone for searchong severals hours for a
> solution as i did.
>
>
> Cheers,
> Torben
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=15666#15666
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to