Exception thrown at 0x02EEC660 (nvoglv32.dll) in CobwebDiagram.exe: 0xC0000005: Access violation reading location 0x00000000

Hello, so I got an Expectation error about nvoglv32.dll. i don’t know what is the problem, can anyone help me to fix it?

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

using namespace std;

unsigned int createShader(unsigned int shadertype, const char* shaderSource) {
  unsigned int shader;
  shader = glCreateShader(shadertype);
  glShaderSource(shader, 1, &shaderSource, NULL);
  glCompileShader(shader);

  int status;
  char info[512];
  glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  if (!status) {
    glGetShaderInfoLog(shader, 512, NULL, info);
    cout << "FAILED TO COMPILE!\n" << info << '\n';
    glDeleteShader(shader);
  }

  return shader;
}

unsigned int createShaderProgram(const char* VshaderSrc, const char* FshaderSrc) {
  unsigned int vertexShader = createShader(GL_VERTEX_SHADER, FshaderSrc);
  unsigned int fragmentShader = createShader(GL_FRAGMENT_SHADER, FshaderSrc);

  unsigned int shaderProgram;
  shaderProgram = glCreateProgram();

  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);

  int status;
  char info[512];
  glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
  if (!status) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, info);
    cout << "FAILED TO LINK PROGRAM\n" << info << '\n';
  }

  return shaderProgram;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
  glViewport(0, 0, width, height);
}

void closeWindow(GLFWwindow* window) {
  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
    glfwSetWindowShouldClose(window, true);
  }
}

void cobwebDiagmramForLogistics(float x0, float miu, float* ptr) {
  *(ptr) = x0;
  *(ptr + 1) = 0.0f;
  *(ptr + 2) = 0.0f;

  *(ptr + 3) = x0;
  *(ptr + 4) = miu * x0 * (1.0f - x0);
  *(ptr + 5) = 0.0f;

  float x = x0;

  for (int i = 1; i < 100; i++) {
    float horPlot = miu * x * (1.0f - x);

    *(ptr + i * 6) = horPlot;
    *(ptr + i * 6 + 1) = horPlot;
    *(ptr + i * 6 + 2) = 0.0f;

    float verPlot = miu * horPlot * (1.0f - horPlot);

    *(ptr + i * 6 + 3) = horPlot;
    *(ptr + i * 6 + 4) = verPlot;
    *(ptr + i * 6 + 5) = 0.0f;

    x = verPlot;
  }
}

int main() {
  float CobwebPlot[606];
  cobwebDiagmramForLogistics(0.2f, 3.6f, &CobwebPlot[0]);
  unsigned int positions[1210];
  for (int i = 0; i < 605; i++) {
    positions[i] = i;
    positions[i + 1] = i + 1;
  }

  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(800, 600, "Cobweb_Diagram", NULL, NULL);

  if (window == NULL) {
    cout << "Failed to create GLFW window" << endl;
    glfwTerminate();
    return -1;
  }

  glfwMakeContextCurrent(window);
  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
      cout << "Failed to initialize GLAD" << endl;
      return -1;
  }

  unsigned int cobwebBuffer;
  unsigned int bufferArray;
  unsigned int elementArray;
  glGenVertexArrays(1, &bufferArray);
  glGenBuffers(1, &cobwebBuffer);
  glGenBuffers(1, &elementArray);

  glBindBuffer(GL_ARRAY_BUFFER, cobwebBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(CobwebPlot), CobwebPlot, GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArray);
  glBufferData(GL_ELEMENT_ARRAY_BARRIER_BIT, sizeof(positions), positions,
               GL_STATIC_DRAW);

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

  const char* vertexShaderSource =
      "#version 330 core\n"
      "layout (location = 0) in vec3 aPos;\n"
      "void main()\n"
      "{\n"
      "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
      "}\0";

  const char* fragmentShaderSource = R"(
      #version 330 core
      out vec4 lineColor;
      void main()
      {
          lineColor = vec4(1.0f, 0.1f, 0.2f, 1.0f);
      }
)";

  unsigned int shaderProgram =
      createShaderProgram(vertexShaderSource, fragmentShaderSource);
  glUseProgram(shaderProgram);

  glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

  while (!glfwWindowShouldClose(window)) {
    closeWindow(window);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(bufferArray);
    glDrawElements(GL_LINE_STRIP, 1210, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwTerminate();
  return 0;
}

You are getting an Access Violation, aka a Segmentation Fault (segfault). This typically happens when trying to de-reference an invalid pointer. The first thing you should do with those is run your program under a debugger to find out where in your code the crash happens. In many cases that will be enough to figure out the underlying cause quite quickly.

Unrelated, but you are only initializing half your indices, you probably want i += 2 in the for loop:

does that for loop create structure like this:
{0, 1, 1, 2, 2, 3 …}

and i still get same error

Are you asking what the loop as you’ve written it does, what it does with the change I suggested, or is this what it’s supposed to do? :wink:
FWIW I was trying to point out that you are only writing to positions[0] through positions[605] and that did not look like it was the intent…

Yes, that’s expected and the reason why I said: “Unrelated, but…”.
Please do the absolute minimal effort debugging on this and tell us on which line of your code the crash is. You will be far more productive if you can learn to diagnose these kinds of problems on your own and finding out where exactly the crash happens should be the first step. Even if you cannot find the root cause from that it will make it much more likely that someone here can spot the problem because narrows down which pointers we even have to consider as possibly invalid.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.