|
自己在回调中应用智能指针动态的添加删除场景节点来实现内存的释放,所有需要添加删除的节点都被定义为智能指针,需要添加的节点被加到g_addNode组节点中,需要删除的节点被加入到g_delNode组节点中,g_addNode与g_delNode是随视点不断更新的,每次回调中将节点从根节点中添加或删除,但每次智能指针在执行unref()时老是出错。请高手帮忙给看看,谢谢了!!!以下是回调中的代码:
#pragma once
#include<osg/nodeCallBack>
#include<osg/Group>
#include "stdafx.h"
#include "MFC_OSG_MDI.h"
#include "MFC_OSG_MDIDoc.h"
#include "MFC_OSG_MDIView.h"
extern osg::Group *g_addNode;
extern osg::Group *g_delNode;
extern bool b_IsHugeDataPageOn;
extern CMFC_OSG_MDIView *tepview;
class CSceneUpdate :
public osg::NodeCallback
{
public:
CSceneUpdate(void);
~CSceneUpdate(void);
public:
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if(b_IsHugeDataPageOn==true)
{
long num = 0;
// osg::ref_ptr<osg::Node> ptNode;
//osg::Node *ptNode=NULL;
osg::Group *ptGp = NULL;
ptGp = dynamic_cast<osg::Group *>(node);
if(g_addNode->getNumChildren()>=1)
{
num = g_addNode->getNumChildren();
osg::ref_ptr<osg::Node> ptNode;
{
for(int i = 0;i<num;i++)
{
ptNode= g_addNode->getChild(i);
if(ptGp!=NULL&&tepview->m_pNodes!=NULL)
{
ptGp->addChild(ptNode.get());
tepview->m_pNodes->addChild(ptNode.get());
}
}
num = g_addNode->getNumChildren();
for(int i = num-1;i>=0;i--)
{
ptNode = g_addNode->getChild(i);
if(ptNode.valid())
{
g_addNode->removeChild(ptNode.get());
}
}
}
}
if(g_delNode->getNumChildren()>=1)
{
num = g_delNode->getNumChildren();
osg::ref_ptr<osg::Node> ptNode1;
{
for(int i = num-1;i>=0;i--)
{
ptNode1 = g_delNode->getChild(i);
if(ptNode1.valid()&&tepview->m_pNodes!=NULL)
{
ptGp->removeChild(ptNode1.get());
if(ptNode1.valid()&&ptNode1->referenceCount()>0&& tepview->m_pNodes->containsNode(ptNode1.get()))
tepview->m_pNodes->removeChild(ptNode1.get());
}
}
num = g_delNode->getNumChildren();
for(int i = num-1;i>=0;i--)
{
ptNode1 = g_addNode->getChild(i);
if(ptNode1.valid()&&g_delNode)
g_delNode->removeChild(ptNode1.get());
while(ptNode1.valid()&&ptNode1->referenceCount()>0)
{
ptNode1->unref();
}
}
}
}
traverse(node, nv);
}
}
}; |
|