从特定类调用函数时的分段错误

时间:2011-11-02 15:04:34

标签: c++ opengl

当我尝试从我的shader_id课程中访问Shader时,会发生段错误,但只有当我尝试从我的OBJModel课程中访问它时。如果我将shader_id设为public或者将其设为私有并从函数访问它,则无关紧要。

这是我在运行GDB时遇到的错误:

Program received signal SIGSEGV, Segmentation fault.
0x00404831 in _fu9___ZSt4cout () at .\src/objmodel.cpp:153
153             std::cerr << "Shader ID: " << shader->shader_id << std::endl;

无论何时尝试在类中访问它,甚至在构造函数中都会出现段错误。

OBJModel类相当大,所以我只会发布头文件和构造函数(因为那些是出现错误时唯一执行的文件。)

#ifndef __OBJMODEL_H_INCLUDED
#define __OBJMODEL_H_INCLUDED

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

#include <gl/glew.h>

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include "cache.h"
#include "shader.h"

typedef struct {
    float r;
    float g;
    float b;
} Color;

typedef struct {
    float x;
    float y;
    float z;
} Vertex;

typedef struct {
    float u;
    float v;
} TexCoord;

typedef struct {
    float x;
    float y;
    float z;
} Normal;

typedef struct {
    int vertices[4];
    int texcoords[4];
    int normals[4];
    int face_type;
} Face;

typedef struct {
    std::string filename;
    Color ambient;
    Color diffuse;
    Color specular;
    float specular_power;
    float transparency;
} Material;

typedef struct {
    std::vector<Face> faces;
    unsigned int num_vertices;
    GLfloat* vertices;
    GLfloat* normals;
    GLfloat* texcoords;
    Material material;
    bool has_material;
} Object;

class OBJModel {
    private:
        std::vector<Vertex> vertices;
        std::vector<TexCoord> texcoords;
        std::vector<Normal> normals;
        std::vector<Object> objects;
        std::map<std::string, Material> materials;
        Cache* cache;
        Shader* shader;
    public:
        float xRot;
        float yRot;
        float zRot;
        OBJModel(Cache* cache_, Shader* shader_);
        ~OBJModel();
        int load_mtl(std::string filename);
        int load(std::string filename);
        int expand_indices(unsigned int index);
        int draw_object(Object o);
        int draw_object_old(Object o);
        int draw(float x, float y, float z);
        int draw_immediate(float x, float y, float z);
};

#endif

OBJModel构造函数:

OBJModel::OBJModel(Cache* cache_, Shader* shader_) {
cache = cache_;
shader = shader_;
xRot = 0;
yRot = 0;
zRot = 0;
std::cerr << "Init OBJ ID: " << shader->shader_id << std::endl;
}

这是我的着色器构造函数:

Shader::Shader(std::string vert_filename, std::string frag_filename) {
    vert_shader = glCreateShader(GL_VERTEX_SHADER);
    frag_shader = glCreateShader(GL_FRAGMENT_SHADER);

    const char* vert_source_c = textFileRead(vert_filename.c_str());
    const char* frag_source_c = textFileRead(frag_filename.c_str());

    glShaderSource(vert_shader, 1, &vert_source_c, 0);
    glShaderSource(frag_shader, 1, &frag_source_c, 0);

    glCompileShader(vert_shader);

    const unsigned int BUFFER_SIZE = 512;
    char buffer[BUFFER_SIZE];
    GLsizei length = 0;

    glGetShaderInfoLog(vert_shader, BUFFER_SIZE, &length, buffer);
    if (length > 0) {
        std::cerr << "Shader " << vert_shader << " (" << vert_filename << ") compile error: " << buffer << std::endl;
    }

    glCompileShader(frag_shader);

    const unsigned int BUFFER_SIZE2 = 512;
    char buffer2[BUFFER_SIZE];
    GLsizei length2 = 0;

    glGetShaderInfoLog(frag_shader, BUFFER_SIZE2, &length2, buffer2);
    if (length2 > 0) {
        std::cerr << "Shader " << frag_shader << " (" << frag_filename << ") compile error: " << buffer2 << std::endl;
    }

    shader_id = glCreateProgram();
    glAttachShader(shader_id, vert_shader);
    glAttachShader(shader_id, frag_shader);
    glBindFragDataLocation(shader_id, 0, "fragColor");
    glBindAttribLocation(shader_id, 0, "vertex");
    glBindAttribLocation(shader_id, 1, "normal");
    glBindAttribLocation(shader_id, 2, "texcoord");
    glLinkProgram(shader_id);
}

如果您需要查看更多代码,请告诉我,我会发布。

修改 我像这样创建OBJModel:

island_model = new OBJModel(&cache, shader);
island_model->load("models/island/island_low.obj");

编辑2: 这是Shader类声明:

#ifndef __SHADER_H_INCLUDED
#define __SHADER_H_INCLUDED

#include <iostream>
#include <string>
#include <fstream>
#include <set>

#include <gl/glew.h>

class Shader {
private:
    GLuint vert_shader;
    GLuint frag_shader;
    std::set<int> attribsSet;
public:
    GLuint shader_id;
    Shader(std::string vert_filename, std::string frag_filename);
    ~Shader();
    std::string readShaderSource(std::string filename);
    void bind();
    void unbind();
    void setAttrib(std::string attribName, GLint len, void* data);
    void setUniformMatrix4fv(std::string uniformName, GLfloat* data);
    void reset();
    GLuint id();
};

#endif

2 个答案:

答案 0 :(得分:3)

我似乎意外地在OBJModel之前创建了Shader。改变顺序修复了一切。

答案 1 :(得分:0)

确保shader_不为null或错误的引用(在堆栈上创建的某个对象的地址)。因为它是一个指针,所以确保使用new创建并且Shader对象的所有权是明确的。

OBJModel model = OBJModel(NULL, 
    new Shader(std::string("filename"), std::string("fragname")));
//OBJModel is responsible for deleting Shader in its destructor in this scenario.