声明std :: string会导致OpenGL出现分段错误 [英] Declaration of std::string causes a segmentation fault with OpenGL

查看:102
本文介绍了声明std :: string会导致OpenGL出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个非常简单的OpenGL程序:

Consider a very simple OpenGL program:

#include <GL/glut.h>

static void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Test");
    glutDisplayFunc(RenderScene);
    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
    glutMainLoop();

    return 0;
}

这可以编译并正常运行,按预期显示灰色窗口.

This compiles and runs fine, displaying a grey window as expected.

然后,在进行OpenGL处理​​之前,我将三个变量引入到主函数中.代码变为:

Then, I introduce three variables into the main function, before the OpenGL processing. The code becomes:

#include <string>
#include <GL/glut.h>


static void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
}


int main(int argc, char** argv)
{
    int x = 5;
    char y = 'a';
    std::string z = "abc";

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Test");
    glutDisplayFunc(RenderScene);
    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
    glutMainLoop();

    return 0;
}

编译这很好,但是如果我运行它,则会收到分段错误错误.现在,如果我注释掉std::string z = "abc";行,则它可以正常运行而没有任何错误.

Compiling this is fine, but if I run it, I receive a segmentation fault error. Now, if I comment out the line std::string z = "abc";, then it runs fine without any errors.

因此,由于某种原因,在这里声明字符串变量会导致分段错误,但对于任何其他变量类型都不会.

So, for some reason, declaring a string variable is causing a segmentation fault here, but not with any other variable types.

如果我删除所有OpenGL代码,那么它运行良好.但是,如果我删除除一行以外的所有OpenGL代码,例如glutInit(&argc, argv);,那么它仍然会导致分段错误.

If I remove all the OpenGL code, then it runs fine. But if I remove all the OpenGL code except for just one line, such as glutInit(&argc, argv);, then it still causes the segmentation fault.

有什么想法吗?

推荐答案

我也遇到类似的问题,但是我的问题在Linux上.您使用Linux还是Windows?您是否安装了任何图形卡驱动程序?

I'm having similar problem too, but mine was on Linux. Is your on Linux or Windows? Do you have any graphic card driver install?

对于我来说,我在安装了nVidiaUbuntu上运行,我注意到当我将程序与位于/usr/lib/x86_64-linux-gnulibGL.so链接时,会导致分段错误.然后我还发现位于/usr/lib/nvidia-352的另一个libGL.so,它将链接而不会崩溃.

For my case, I'm running on Ubuntu with nVidia installed, I notice that when I link my program with libGL.so located at /usr/lib/x86_64-linux-gnu, it will cause segmentation fault. Then I also found out there is another libGL.so located at /usr/lib/nvidia-352, this will link without crash.

希望获得帮助.

这篇关于声明std :: string会导致OpenGL出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆