osg智能指针错误 - Warning deleting still referenced object

1 osg智能指针错误 - Warning: deleting still referenced object

1.1 问题描述

类的定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class QOsgWidget {
public:
///< essential widget, use this ptr to be the real widget
osgQOpenGLWidget* pWidget = nullptr;
// QOsgWidget(QWidget* parent = nullptr);
QOsgWidget(const std::string& modelPath, QWidget* parent = nullptr);
~QOsgWidget();

///< osg base vars
osg::ref_ptr<osg::Group> mRoot = nullptr; // root node of the osg scene
osg::ref_ptr<osg::Camera> camera = nullptr; // osg camera
osg::ref_ptr<osgViewer::Viewer> mViewer = nullptr; // osg viewer
osg::ref_ptr<osgGA::TrackballManipulator> trackball = nullptr;
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = nullptr;
osg::ref_ptr<osgViewer::StatsHandler> stats = nullptr;
<被省略>
}

类的析构函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
QOsgWidget::~QOsgWidget() {
// end the render thread and destroy the osgOpenglWidget
std::this_thread::sleep_for(std::chrono::microseconds(1000));
mViewer->setDone(true);
std::this_thread::sleep_for(std::chrono::microseconds(1000));
mViewer->stopThreading();

//////////////////////////////////////////////////////////////////////////
/// without folowing post process, u will crash with the bellowing warning when: delete pWidget
/// Warning: deleting still referenced object 000002433CA59BB0 of type 'class osg::Referenced * __ptr64'
/// the final reference count was 1, memory corruption possible.
///
/// the reason i guess is:
/// the bellowing vars can not be automatically set to null when delete pWidget, because they does
/// not belong to the pWidget, so u need to set them to null manually, otherwise when delete pWidget,
/// the destructor of pWidget will try to free the bellowing vars, but their ref != 0
///////////////////////////////// PART 1 /////////////////////////////////
mRoot = nullptr;
mViewer = nullptr;
camera = nullptr;
trackball = nullptr;
keyswitchManipulator = nullptr;
//////////////////////////////////////////////////////////////////////////

delete pWidget; // call the destructor of the osgOpenglWidge
}

问题描述,在执行类的析构的时候:
当没有PART1的时候,直接执行pWidget会出错

1.2 原因推测:

  • 1 osg::ref_ptr<> 是一种智能指针,自动计算指针的引用个数,当引用个数为0的时候,自动回收其指向的对象。
  • 2 于是出现一个大问题,当pWidget这个类指针所指向的类,里面很多组件(比如 camera,mViewer)是在QOsgWidget类中申明的,那么当析构pWidget的时候,它会去析构自己的组件,然后当它析构了自己类里面的那些指针后,发现QOsgWidget里面的智能指针不是null(这些指针很可能在被别的模块访问),所以引用计数不为0,所以无法删除,或者删除会crash

1.3 得出结论:

  • 1 如果类mon里面嵌套了类son,那么当mon的析构的时候,要注意如下两点:
    • 1.1 将son析构
    • 1.2 执行son的析构前,对son的析构可能产生影响的内存,指针,都要释放掉和置空