我正在处理一个非常奇怪的问题,所以我写了一个最小的例子来重现它
首先,我从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();
为了验证这一点,是的,这里出现了错误。但为什么呢?我错过了什么?
解决方案是强制QGLWidget
兼容配置文件:
format.setProfile( QGLFormat::Compatibility );
虽然我不明白glGenSamplers
为什么需要这个。据我所知,从3.3开始,这个功能实际上应该是核心配置文件的一部分。