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

6 篇文章 1 订阅

1.1 问题描述

类的定义如下:

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;
    <被省略>
}

类的析构函数如下:

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的析构可能产生影响的内存,指针,都要释放掉和置空
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值