Hi,
valgrind is very powerfull if you're on an Linux/Unix environnement, if
you are using MSVC you can use this:

add this bloc at the beginning of each file you want to trace (after all
includes):
#ifdef _WIN32 
# ifdef _MSC_VER
# ifdef _DEBUG
# include <crtdbg.h>
# undef THIS_FILE
static char THIS_FILE[] = __FILE__;
# define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define calloc(s) _calloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define realloc(s) _recalloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
# define delete delete(_NORMAL_BLOCK, __FILE__, __LINE__)
# endif /* _DEBUG */
# endif /* _MSC_VER */
#endif /* _WIN32 */

It replace all allocation routines by the ctr_dbg equivalent.

then add this code in the main to set the proper dbg flags:
// Look at http://msdn.microsoft.com/en-us/library/5at7yxcs.aspx
// for more information about the flags
#ifdef _DEBUG
int flag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
flag |= _CRTDBG_LEAK_CHECK_DF |_CRTDBG_CHECK_ALWAYS_DF ;
_CrtSetDbgFlag(flag);
#endif 

when you stop debugging you program, it make a memory leaks report in
the msvc output console...

There're also some other tools that look interesting, but I've never
used:
http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
http://www.cbmamiga.demon.co.uk/mpatrol/

my 2 cents, 

    Pierre.


Le jeudi 18 décembre 2008 à 18:21 -0500, Jean-Sébastien Guay a écrit : 

> Hello all,
> 
> I am currently chasing memory leaks in our application, which is like a 
> modeling tool where the user will open/work on/save/close data files 
> multiple times during the same application run. What we are seeing is 
> that when a file is closed, not all memory related to scene graph 
> objects seems to be released.
> 
> First question:
> 
> I enabled the DEBUG_OBJECT_ALLOCATION_DESTRUCTION define in 
> src/osg/Referenced.cpp, and have set up a small test which just creates 
> two viewers one after the other in different scopes. Pseudocode:
> 
> int main(int argc, char** argv)
> {
>      int bob = 1;    // breakpoint #1
> 
>      {
>          osgViewer::Viewer viewer;
>          osg::ref_ptr<osg::Node> loadedModel =
>              osgDB::readNodeFile("cow.osg");
>          viewer.setSceneData( loadedModel.get() );
>          viewer.run();
>      }
> 
>      bob = 2;    // breakpoint #2
> 
>      {
>          osgViewer::Viewer viewer;
>          osg::ref_ptr<osg::Node> loadedModel =
>              osgDB::readNodeFile("cow.osg");
>          viewer.setSceneData( loadedModel.get() );
>          viewer.run();
>      }
> 
>      bob = 3;    // breakpoint #3
> }
> 
> What I see is this (all in debug):
> 
> Breakpoint #1     :    8 Referenced objects,  12MB
> First viewer run  : ~505 Referenced objects, 266MB
> Breakpoint #2     :  247 Referenced objects, 167MB
> Second viewer run : ~505 Referenced objects, 266MB
> Breakpoint #3     :  247 Referenced objects, 172MB
> 
> But then, when I pass breakpoint #3, all the destructors for those 
> Referenced objects are called, and at the end no objects are left 
> allocated. (I can see that the final number of objects is 0)
> 
> Other than seeing that there are no real "leaks" per se, my question 
> about that is, what are those objects that are staying alive outside the 
> scopes? I would have thought that most objects would only live in one of 
> the viewer scopes above, 247 seems pretty large to me...
> 
> Second question:
> 
> Our application apparently has memory leaks, because when closing a file 
> and opening the same one again, the application takes about 20MB more 
> each time (i.e. memory usage is ~100MB with a file open, then ~50MB when 
> it's closed, then ~120MB when it's opened again, then ~70MB when it's 
> closed again, then ~140MB when it's opened again, and so on).
> 
> However, running IBM Purify on the application reveals no massive leaks, 
> only a few false positives. Are there any tools or techniques that 
> someone could recommend to make finding and fixing memory leaks easy, 
> and which work well with OSG? I tried Visual Leak Detector which is open 
> source, but it reported so many false positives related to variables 
> stored in ref_ptrs (over 400) that I gave up on it.
> 
> Thanks in advance,
> 
> J-S

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pierre BOURDIN
I.M.E.R.I.R.
Av. Pascot BP 90443
66004 PERPIGNAN
tél: 04 68 56 80 18
fax: 04 68 55 03 86
email: bour...@imerir.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to