从特定类调用函数时出现分段错误
当我尝试从 Shader
类访问 shader_id
时,它会出现段错误,但仅当我尝试从 OBJModel
类访问它时。如果我将 Shader_id 设为公开,或者将其设为私有并从函数访问它,这并不重要。
这是我在运行 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: 这是着色器类声明:
#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
When I try to access shader_id
from my Shader
class, it segfaults, but only when I try to access it from my OBJModel
class. It doesn't matter if I make shader_id public or if I make it private and access it from a function.
This is the error I get when running 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;
It segfaults no matter when I try to access it in the class, even in the constructor.
The OBJModel class is rather big so I will only be posting the header file and constructor (as those are the only ones that have been executed when the error appears.)
#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 constructor:
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;
}
This is my shader constructor:
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);
}
If you need to see more code, please tell me and I'll post it.
Edit:
I create the OBJModel like this:
island_model = new OBJModel(&cache, shader);
island_model->load("models/island/island_low.obj");
Edit 2:
This is the Shader class declaration:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来我不小心在
Shader
之前创建了OBJModel
。改变顺序就解决了一切。It seems I accidentally created the
OBJModel
before theShader
. Changing the order fixed everything.确保
shader_
不为 null 或错误引用(在堆栈上创建的某个对象的地址)。由于它是一个指针,请确保它是使用 new 创建的,并且 Shader 对象的所有权是明确的。Make sure that
shader_
is not null or a bad reference (adress of some object created on the stack). Since it is a pointer be sure that is created withnew
and that ownership of theShader
object is clear.