我遇到了一个非常奇怪的问题,所以我写了一个简单的例子来重现它。
首先,我从QGLWidget
派生一个班级:
class Demo : public QGLWidget
{
public:
struct error {};
explicit Demo( const QGLFormat& format ) : QGLWidget( format )
{
}
protected:
virtual void initializeGL() override
{
const GLenum glewState = glewInit();
if( glewState != GLEW_OK ) throw error();
unsigned int id;
glGenSamplers( 1, &id ); //<-- Note this here!
}
};
这是main
函数:
int main( int argc, char** argv )
{
QApplication app( argc, argv );
QGLFormat format = QGLFormat::defaultFormat();
format.setVersion( 3, 3 );
Demo demo( format );
demo.show();
return QApplication::exec();
}
我使用G ++ 4.9构建此代码。当我使用GDB运行它时,我得到的是:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x0000000000402add in Demo::initializeGL() () at test.cpp:16
...
显然,函数glGenSamplers
指向0
。我还添加了另一行
if( glGenSamplers == nullptr ) throw error();
验证这一点,是的,这里引发了错误。但为什么?我错过了什么?
答案 0 :(得分:0)
解决方案是强制QGLWidget
兼容性配置文件:
format.setProfile( QGLFormat::Compatibility );
虽然我不明白为什么glGenSamplers
需要这个。据我所知,从3.3开始,这个函数实际上应该是核心配置文件的一部分。