3D进阶之OSG: Qt程序退出时在QScopedPointer中崩溃

这篇文章讲述了如何解决Qt应用程序在退出时由于QGLContext错误导致的崩溃问题,涉及源码修改以避免空指针异常,适用于osgQt 旧版本。关键在于在调用makeCurrent前检查OpenGL上下文的valid性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天调试一个osgQt的应用程序,在程序退出时发生崩溃。使用的是Qt5.15.0。

查了一下,原因在这里,

[QTBUG-93173] QGLContext::makeCurrent() crashes due to d->guiGlContext being null - Qt Bug Tracker

最新版本应该已经修复了这个问题。

原因:

在程序运行过程中,主程序会不断轮询所有的widget的,并传递消息。但在退出时,glcontext其实已经在析构,此时碰到没有nullptr检查,再次把widget设置为当前用例时,就会崩溃。

解决办法:

原作者是这么说的:

At the application level the following check can be made:

auto *_context = context()->contextHandle();
if (nullptr == _context || !_context->isValid())
{
  return ;
}

问题是:我们要怎么改?

接上一篇文章:3D进阶之OSG:编译osgQt的旧版本_高精度计算机视觉的博客-CSDN博客

老版本的osgQt源码在这里:https://github.com/mathieu/osgQt 

找到函数


bool GraphicsWindowQt::makeCurrentImplementation()
{
    if (_widget->getNumDeferredEvents() > 0)
        _widget->processDeferredEvents();

    _widget->makeCurrent();    

    return true;
}

修改后的源码如下,

bool GraphicsWindowQt::makeCurrentImplementation()
{
    if (_widget->getNumDeferredEvents() > 0)
        _widget->processDeferredEvents();

    /*
    https://bugreports.qt.io/browse/QTBUG-93173
    auto* _context = _widget->context()->contextHandle();
    if (nullptr == _context || !_context->isValid()){
        return;
    }
    */
    QOpenGLContext* _context = _widget->context()->contextHandle();
    if (nullptr == _context || !_context->isValid())
    {
        return false;
    }

    _widget->makeCurrent();    

    return true;
}

最后,更新修改后的源码在这里,

GitHub - SpaceView/osgQtOld: Updated old version of osgQt from https://github.com/mathieu/osgQt

本文结束

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值