0

My goal is using library OSGEarth to make a MFC project that can display the model "openstreetmap.earth". I finished this and can see the the earth.But every time when i close the project, the output window in vs2015 say there are memory leaks in the program.

Here is the window output:

Detected memory leaks!
Dumping objects ->
{306240} normal block at 0x00000000076902F0, 16 bytes long.
Data: <0,i             > 30 2C 69 07 00 00 00 00 00 00 00 00 00 00 00 00 
{306239} normal block at 0x0000000007692C30, 9 bytes long.
Data: <Pragma:  > 50 72 61 67 6D 61 3A 20 00 
{303648} normal block at 0x0000000007693040, 16 bytes long.
Data: < 5i             > 90 35 69 07 00 00 00 00 00 00 00 00 00 00 00 00 
{303647} normal block at 0x0000000007693590, 9 bytes long.
Data: <Pragma:  > 50 72 61 67 6D 61 3A 20 00 
{301180} normal block at 0x00000000076938B0, 16 bytes long.
 Data: <`8i             > 60 38 69 07 00 00 00 00 00 00 00 00 00 00 00 00 
{301179} normal block at 0x0000000007693860, 9 bytes long.
 Data: <Pragma:  > 50 72 61 67 6D 61 3A 20 00 
{297799} normal block at 0x0000000007691060, 16 bytes long.
 Data: <  i             > 10 10 69 07 00 00 00 00 00 00 00 00 00 00 00 00 

I examined the program and found that when I delete this code m_Model = osgDB::readNodeFile(m_strModelName); there is no more memory leaks.

void COSGEarth::InitSceneGraph(void)
{
    // Init the main Root Node/Group
    m_Root = new osg::Group;
    // Load the Model from the model name, 
    //delete below line, no memory leak
    m_Model = osgDB::readNodeFile(m_strModelName);
    if (!m_Model) return;

    // Optimize the model
    osgUtil::Optimizer optimizer;
    optimizer.optimize(m_Model.get());
    optimizer.reset();

    // Add the model to the scene
    m_Root->addChild(m_Model.get());

}

I defined m_Model as osg::ref_ptr<osg::Node> m_Model. This is Intelligent pointer.

Why there are memory leaks and how I can solve this issue?

Here is source code :http://bbs.osgchina.org/forum.php?mod=attachment&aid=NzIwNnwzZWYxZDIyZjlhOGY1MWFjZjhiNGFiMWYwMTc5YmJlNXwxNTEyMzc5ODE2&request=yes&_f=.zip

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
zalisc
  • 1
  • 1
  • What do you mean with 'shield this code'? Sure that the COSGEartch destructor is called. Sure that the OSG code doesn't use any further caching? Use _crtBreakAlloc to check what is allocated there! – xMRi Dec 04 '17 at 09:50
  • @xMRi. I mean when i delete this line "m_Model = osgDB::readNodeFile(m_strModelName);", where is no memory leaks any more. COSGEartch destructor is called. – zalisc Dec 04 '17 at 10:16
  • So there might be some caching. Maybe reading the node file allocates some global static variables. – xMRi Dec 04 '17 at 10:17

1 Answers1

1

I believe these reported "leaks" are false positives. Refer to this thread that explains why:

http://forum.openscenegraph.org/viewtopic.php?t=1475

XenonofArcticus
  • 573
  • 2
  • 7
  • Visual Studio reports all memory still allocated at exit. This includes statically allocated memory and memory which is still in active use but has not been freed. This can result in a lot of false positives. – Damian Dixon Oct 11 '18 at 11:57