【发布时间】:2015-03-20 15:02:01
【问题描述】:
我遇到了一个非常奇怪的问题,所以我写了一个最小的例子来重现它。
首先我从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();
验证这一点,是的,这里出现了错误。但为什么?我错过了什么?
【问题讨论】: