C++ OpenGL Frame not in module

  access-violation, c++, glfw, opengl

I am getting frame not in module errors. The exception thrown says

Exception thrown at 0x0000022D4B05FEEE in DepthTesting.exe: 0xC0000005: Access violation reading location 0x0000022D4B254001.

I have tried ticking the common language runtime exceptions setting in the exception settings but that has not fixed the problem. In the beginning, I would be able to run successfully every four runs but now it never runs successfully. I have attached the error log and the main.cpp file.

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

#include <Model.h>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <Shader.h>
#include <Camera.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
unsigned int loadTexture(const char* path);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = (float)SCR_WIDTH / 2.0;
float lastY = (float)SCR_HEIGHT / 2.0;
bool firstMouse = true;

// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetScrollCallback(window, scroll_callback);

    // tell GLFW to capture our mouse
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // configure global opengl state
    // -----------------------------
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_ALWAYS); // always pass the depth test (same effect as glDisable(GL_DEPTH_TEST))

    // build and compile shaders
    // -------------------------
    Shader shader("src/depth_testing.vs", "src/depth_testing.fs");

    // set up vertex data (and buffer(s)) and configure vertex attributes
    // ------------------------------------------------------------------
    float cubeVertices[] = {
        // positions          // texture Coords
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };
    float planeVertices[] = {
        // positions          // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
         5.0f, -0.5f,  5.0f,  2.0f, 0.0f,
        -5.0f, -0.5f,  5.0f,  0.0f, 0.0f,
        -5.0f, -0.5f, -5.0f,  0.0f, 2.0f,

         5.0f, -0.5f,  5.0f,  2.0f, 0.0f,
        -5.0f, -0.5f, -5.0f,  0.0f, 2.0f,
         5.0f, -0.5f, -5.0f,  2.0f, 2.0f
    };
    // cube VAO
    unsigned int cubeVAO, cubeVBO;
    glGenVertexArrays(1, &cubeVAO);
    glGenBuffers(1, &cubeVBO);
    glBindVertexArray(cubeVAO);
    glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glBindVertexArray(0);
    // plane VAO
    unsigned int planeVAO, planeVBO;
    glGenVertexArrays(1, &planeVAO);
    glGenBuffers(1, &planeVBO);
    glBindVertexArray(planeVAO);
    glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glBindVertexArray(0);

    // load textures
    // -------------
    unsigned int cubeTexture = loadTexture("resources/textures/companion.png");
    unsigned int floorTexture = loadTexture("resources/textures/metal.jpg");

    // shader configuration
    // --------------------
    shader.use();
    shader.setInt("texture1", 0);

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // per-frame time logic
        // --------------------
        float currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        shader.use();
        glm::mat4 model = glm::mat4(1.0f);
        glm::mat4 view = camera.GetViewMatrix();
        glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
        shader.setMat4("view", view);
        shader.setMat4("projection", projection);
        // cubes
        glBindVertexArray(cubeVAO);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, cubeTexture);
        model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
        shader.setMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        model = glm::mat4(1.0f);
        model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
        shader.setMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        // floor
        glBindVertexArray(planeVAO);
        glBindTexture(GL_TEXTURE_2D, floorTexture);
        shader.setMat4("model", glm::mat4(1.0f));
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // optional: de-allocate all resources once they've outlived their purpose:
    // ------------------------------------------------------------------------
    glDeleteVertexArrays(1, &cubeVAO);
    glDeleteVertexArrays(1, &planeVAO);
    glDeleteBuffers(1, &cubeVBO);
    glDeleteBuffers(1, &planeVBO);

    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);

    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        camera.ProcessKeyboard(FORWARD, deltaTime);
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        camera.ProcessKeyboard(BACKWARD, deltaTime);
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        camera.ProcessKeyboard(LEFT, deltaTime);
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        camera.ProcessKeyboard(RIGHT, deltaTime);
    if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
        camera.ProcessKeyboard(UP, deltaTime);
    if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
        camera.ProcessKeyboard(DOWN, deltaTime);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }

    float xoffset = xpos - lastX;
    float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top

    lastX = xpos;
    lastY = ypos;

    camera.ProcessMouseMovement(xoffset, yoffset);
}

// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
    camera.ProcessMouseScroll(yoffset);
}

// utility function for loading a 2D texture from file
// ---------------------------------------------------
unsigned int loadTexture(char const* path)
{
    unsigned int textureID;
    glGenTextures(1, &textureID);

    int width, height, nrComponents;
    unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
    if (data)
    {
        GLenum format;
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;

        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        stbi_image_free(data);
    }
    else
    {
        std::cout << "Texture failed to load at path: " << path << std::endl;
        stbi_image_free(data);
    }

    return textureID;
}
'DepthTesting.exe' (Win32): Loaded 'C:UserscalebDocumentsOpenGLAdvancedOpenGLx64DebugDepthTesting.exe'. Symbols loaded.
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ntdll.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32kernel32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:Program FilesAVGAntivirusaswhook.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32KernelBase.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32user32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32win32u.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32gdi32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32gdi32full.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32msvcp_win.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ucrtbase.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32shell32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32msvcp140d.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32vcruntime140d.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32vcruntime140_1d.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ucrtbased.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32imm32.dll'. 
The thread 0x46b8 has exited with code 0 (0x0).
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32uxtheme.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32msvcrt.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32combase.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32rpcrt4.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32winmm.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32dinput8.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32advapi32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32sechost.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32XInput1_4.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32cfgmgr32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32devobj.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32dwmapi.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32InputHost.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32SHCore.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32CoreMessaging.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ws2_32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32oleaut32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32propsys.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32WinTypes.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32bcryptprimitives.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32CoreUIComponents.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32kernel.appcore.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32kernel.appcore.dll'. 
'DepthTesting.exe' (Win32): Unloaded 'C:WindowsSystem32kernel.appcore.dll'
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ntmarta.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32msctf.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32hid.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32setupapi.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32bcrypt.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32wintrust.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32crypt32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32msasn1.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32opengl32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32glu32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32clbcatq.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32AppXDeploymentClient.dll'. 
'DepthTesting.exe' (Win32): Unloaded 'C:WindowsSystem32AppXDeploymentClient.dll'
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32DriverStoreFileRepositorynvlti.inf_amd64_3d3405e2f3440970nvoglv64.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32ole32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32version.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32wtsapi32.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32cryptnet.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32cryptbase.dll'. 
The thread 0x57c8 has exited with code 0 (0x0).
The thread 0x5450 has exited with code 0 (0x0).
The thread 0x5130 has exited with code 0 (0x0).
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32windows.storage.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32wldp.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32shlwapi.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32DXCore.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32nvspcap64.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32profapi.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32powrprof.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32umpdc.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32winsta.dll'. 
'DepthTesting.exe' (Win32): Loaded 'C:WindowsSystem32TextInputFramework.dll'. 
Exception thrown at 0x0000028A71C5FEEE in DepthTesting.exe: 0xC0000005: Access violation reading location 0x0000028A71E54000.

The program '[22348] DepthTesting.exe' has exited with code 0 (0x0).

Source: Windows Questions C++

LEAVE A COMMENT