0

我有应用程序,其中代码的关键部分是:

主文件

#include <QApplication>
#include <QSurfaceFormat>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //the same version of context for all GL widgets
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 3);
    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);

    //creation and displaying main window
    MainWindow w(&argc, argv);
    w.show();

    return a.exec();
}

其中MainWindow w使用class GLRendWindow : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core了具有如此重要部分的内容:

void GLRendWindow::initializeGL()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    initializeOpenGLFunctions();

    generateShaders();

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
}

void GLRendWindow::generateShaders()
{
    shaderPoint = Shader("../shader/point.vert", "../shader/point.frag");
    shaderCross = Shader("../shader/cross.vert", "../shader/cross.frag");
}

着色器.h 是:

#ifndef SHADER_H
#define SHADER_H

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <QOpenGLFunctions_3_3_Core>

class Shader: protected QOpenGLFunctions_3_3_Core
{
    public:
        Shader();
        Shader(const char *vertexPath, const char *fragmentPath);

        GLint getID();

    private:
        GLint ID;
        //QOpenGLFunctions_3_3_Core *fun;

    private:
        void checkCompilationStatus(GLuint compiled, bool shader = true);
};
#endif //SHADER_H

Shader.cpp 部分是:

Shader::Shader()
{
}

Shader::Shader(const char *vertexPath, const char *fragmentPath)
{
    //fun = new QOpenGLFunctions_3_3_Core();
    //fun->initializeOpenGLFunctions();

    std::string vertexCode;
    std::string fragmentCode;
    std::ifstream vShaderFile;
    std::ifstream fShaderFile;

    vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    try
    {
        vShaderFile.open(vertexPath);
        fShaderFile.open(fragmentPath);
        std::stringstream vShaderStream, fShaderStream;

        vShaderStream << vShaderFile.rdbuf();
        fShaderStream << fShaderFile.rdbuf();

        vShaderFile.close();
        fShaderFile.close();

        vertexCode = vShaderStream.str();
        fragmentCode = fShaderStream.str();
    }
    catch (std::ifstream::failure &e)
    {
        std::cout << "ERROR: Can't load shader! Path: " << vertexPath << "  " << fragmentPath << std::endl;
    }
    const char *vShaderCode = vertexCode.c_str();
    const char *fShaderCode = fragmentCode.c_str();

    GLint vertex, fragment;

    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &vShaderCode, NULL);
    glCompileShader(vertex);
    checkCompilationStatus(vertex);

    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &fShaderCode, NULL);
    glCompileShader(fragment);
    checkCompilationStatus(fragment);

    ID = glCreateProgram();
    glAttachShader(ID, vertex);
    glAttachShader(ID, fragment);
    glLinkProgram(ID);
    checkCompilationStatus(ID, false);

    glDeleteShader(vertex);
    glDeleteShader(fragment);
}

此代码已正确编译和构建。我可以成功运行应用程序,在着色器创建期间没有错误。它还可以正确显示所有内容。在关闭应用程序期间,我在Shader使用堆栈的析构函数结束后看到分段错误:

libQt5Gui.so.5!QOpenGLContext::removeExternalFunctions(QAbstractOpenGLFunctions*) (Unknown Source:0)
libQt5Gui.so.5!QAbstractOpenGLFunctions::~QAbstractOpenGLFunctions() (Unknown Source:0)
Shader::~Shader(Shader * const this) (/home/rom/repos/radarVis/shader/shader.h:10)
GLRendWindow::~GLRendWindow(GLRendWindow * const this) (/home/rom/repos/radarVis/src/gui/glrendwind.cpp:18)
GLRendWindow::~GLRendWindow(GLRendWindow * const this) (/home/rom/repos/radarVis/src/gui/glrendwind.cpp:22)
libQt5Core.so.5!QObjectPrivate::deleteChildren() (Unknown Source:0)
libQt5Widgets.so.5!QWidget::~QWidget() (Unknown Source:0)
libQt5Widgets.so.5!QWidget::~QWidget() (Unknown Source:0)
libQt5Core.so.5!QObjectPrivate::deleteChildren() (Unknown Source:0)
libQt5Widgets.so.5!QWidget::~QWidget() (Unknown Source:0)
MainWindow::~MainWindow(MainWindow * const this) (/home/rom/repos/radarVis/src/gui/mainwind.cpp:26)
main(int argc, char ** argv) (/home/rom/repos/radarVis/src/main.cpp:23)

Shader如果不再派生自 ,则不会出现此问题QOpenGLFunctions_3_3_Core

我的问题是为什么?
在这种情况下,可能是连接/依赖/其他类派生QOpenGLFunctions_3_3_Core并调用具有分段错误的析构函数。
是否有可能两种机制试图清理我的着色器?或者可能有强制的方法来清理QOpenGLFunctions_3_3_Core我在文档中遗漏的所有内容?

4

0 回答 0