Categories
Development

Exception thrown at 0x00AEA76D in open_gl_prj_1.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0

I work on visual studio community , when i run the code it shows a exception handling error
there are no other existing errors i tried changing the exception handling setting in debug menu on visual studio community , nothing changed . I have no clue about the exception either.

include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
#include <fstream>
#include<string>
#include<sstream>

using namespace std; 

struct shaderprogramsource
{
 string vertex_source;
 string fragment_source;
};

static shaderprogramsource parse_shader(const string &filepath)
{

 ifstream stream(filepath);

 enum class ShaderType
 {
     NONE = -1 , VERTEX = 0 , FRAGMENT = 1
 };

 string line;
 stringstream ss[2];
 ShaderType type = ShaderType::NONE;

 while (getline(stream, line))
 {
     if (line.find("#shader") != string::npos)
     {
         if (line.find("vertex") != std::string::npos)
         {
             type = ShaderType::VERTEX;
         }
         else
             if (line.find("fragment") != std::string::npos)
             {
                 type = ShaderType::FRAGMENT;
             }
     }
     else
     {
         ss[(int)type]<<line<<'\n';
     }
 }

 return {ss[0].str() , ss[1].str()};
}

static unsigned int CompileShader(unsigned int type , const string& source)
{

 unsigned int id = glCreateShader(type);
 const char* src = source.c_str();
 glShaderSource(id, 1, &src, nullptr);
 glCompileShader(id);


 int result;
 glGetShaderiv(id, GL_COMPILE_STATUS, &result);

 if (result == GL_FALSE)
 {

     int length; 
     glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
     char* message = (char *)alloca(length * sizeof(char));
     glGetShaderInfoLog(id, length, &length, message);

     cout << "failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "shader") << "\n";
     cout << message; 

     glDeleteShader(id);

     return 0;

 }

 return id; 

}

static unsigned int create_shader(const string& vertex_shaders, const string& fragment_shaders)
{

 unsigned int program = glCreateProgram();

 unsigned int vs = CompileShader(GL_VERTEX_SHADER , vertex_shaders);
 unsigned int fs = CompileShader(GL_FRAGMENT_SHADER , fragment_shaders);


 glAttachShader(program, vs);
 glAttachShader(program, fs); 
 glLinkProgram(program);
 glValidateProgram(program);

 glDeleteShader(vs);
 glDeleteShader(fs);

 return program;

}

int main(void)


{
 if (!glfwInit())
 {
     cout << "failed to intitialize opengl ! " << "\n";
 }
 else
 {

     GLFWwindow* window = glfwCreateWindow(640, 480, "window", NULL, NULL);

     if (window == NULL)
     {
         cout << "cannot open window !" << "\n";
     }

     glfwMakeContextCurrent(window);

     if (glewInit() != GLEW_OK)
     {
         cout << "failded to initialize GLEW" << "\n";
     }

     static const GLfloat vertices[] = {
         -1.0f,-1.0f,0.0f,
         1.0f,-1.0f,0.0f,
         0.0f,1.0f,0.0f
     };


     GLuint vertexbuffer;                                                        
     glGenBuffers(1, &vertexbuffer);                                              
     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);                                
     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);   


     glEnableVertexAttribArray(0);
     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, 0);

     glBindBuffer(GL_ARRAY_BUFFER, 0);

     shaderprogramsource source = parse_shader("res/shaders/basic.shader");

     unsigned int shader = create_shader(source.vertex_source, source.fragment_source);
     glUseProgram(shader);

     while (!glfwWindowShouldClose(window))
     {
         glClear(GL_COLOR_BUFFER_BIT);

         glDrawArrays(GL_TRIANGLES, 0, 3);


         glfwSwapBuffers(window);
         glfwPollEvents();

     }

 }

}

is there any way to skip this exception handling or fix it .

Leave a Reply

Your email address will not be published. Required fields are marked *