Getting access violation exception in Visual Studi

2019-07-24 10:06发布

问题:

I have been having this problem for a while and I cant for the love of me find a solution.

I want to render a simple triangle. But i keep getting this output in visual studio when compiling the program.

NOTE> I do not belive it is not a linking problem but something else. I have checked my linker countless times and everything is there!

LINK: https://pastebin.com/xeTDd0Qu

main

static const GLfloat g_vertex_buffer_data[] = {
    100.0f, 100.0f, 0.0f,
    150.0f, 100.0f, 0.0f,
    100.0f, 150.0f, 0.0f,
};



GLFWwindow* window;
window = initWindow(640, 480, "Title");


GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);


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

while (!glfwWindowShouldClose(window)) {

    glViewport(0, 0, 640, 480);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);

    glFlush();

    glfwSwapBuffers(window);
    glfwPollEvents();
}

glfwTerminate();

return 0;

initWindow()

GLFWwindow* initWindow(int a_width, int a_height, const char* title) {
glewExperimental = GL_TRUE;
int err = glewInit();
if (!err) {
    exit(-1);
}

if (!glfwInit()) {
    printf("glfwInit() failed!");
    return nullptr;
}

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

if (!window) {
    glfwTerminate();
    return nullptr;
}

return window;

}

Thanks!

EDIT: Exception message i get: Exception thrown at 0x00000000 in ConvexHullVisualiser.exe: 0xC0000005: Access violation executing location 0x00000000.

回答1:

The error you get tells you that you are trying to execute a function pointer that points to NULL. Most OpenGL functions are (on windows) function pointers and are loaded at runtime. In total, this means you are trying to execute an OpenGL function that has not been loaded.

Most probably, this happens because GLEW can only be initialized successfully if there is a valid OpenGL context present. Since the context is created by glfwCreateWindow, glewInit has to be called after this line.

You are also missing a call to glfwMakeContextCurrent to bind the OpenGL context to the active thread.

if (!glfwInit()) {
    printf("glfwInit() failed!");
    return nullptr;
}

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

if (!window) {
    glfwTerminate();
    return nullptr;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
int err = glewInit();
if (!err) {
    exit(-1);
}

Note, that glewInit does not return an int but a GLenum. The correct error check should looks somehow like this:

GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  ...
}

Source: GLEW Documentation