Search code examples
c++openglglfwimgui

How to fix memory access violation with OpenGL glViewport?


I am trying to build an application in C++ with ImGUI, OpenGL and GLFW.

However, when i try to run my program, i get a white screen that then crashes and gives me an error code of -1073741819.

Here is my Class for creating a window, alot of the stuff i copied from the ImGUI opengl3 example:


#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_internal.h>
#include "imgui.h"
#include "AppWindow.h"

static void glfw_error_callback(int error, const char* description)
{
    fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}

AppWindow::AppWindow(const char* title, int width, int height)
{
    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit())
        return ;

    // Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
    // GL ES 2.0 + GLSL 100
    const char* glsl_version = "#version 100";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
    // GL 3.2 + GLSL 150
    const char* glsl_version = "#version 150";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // Required on Mac
#else
    // GL 3.0 + GLSL 130
    const char* glsl_version = "#version 130";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // 3.0+ only
#endif

    // Create window with graphics context
    window = glfwCreateWindow(width, height, title, nullptr, nullptr);
    if (window == nullptr)
        return ;
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync

    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    io = ImGui::GetIO(); (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
    io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;         // Enable Docking
    io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;       // Enable Multi-Viewport / Platform Windows
    //io.ConfigViewportsNoAutoMerge = true;
    //io.ConfigViewportsNoTaskBarIcon = true;

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsLight();

    // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
    ImGuiStyle& style = ImGui::GetStyle();
    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
    {
        style.WindowRounding = 0.0f;
        style.Colors[ImGuiCol_WindowBg].w = 1.0f;
    }

    // Setup Platform/Renderer backends
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init(glsl_version);

    // Our state
    bool show_demo_window = true;
    bool show_another_window = false;


}

AppWindow::~AppWindow()
{

#ifdef __EMSCRIPTEN__
    EMSCRIPTEN_MAINLOOP_END;
#endif

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);
    glfwTerminate();

    return;
}



void AppWindow::setUpdateFunction(void (*function)())
{
    updateFunction = function;
}

void AppWindow::update()
{
    if (updateFunction != nullptr)
        updateFunction();
}

void AppWindow::run()
{
#ifdef __EMSCRIPTEN__
    // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
    // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
    io.IniFilename = nullptr;
    EMSCRIPTEN_MAINLOOP_BEGIN
#else
    while (!glfwWindowShouldClose(window))
#endif
    {
        // Poll and handle events (inputs, window resize, etc.)
        // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
        // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
        // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
        // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
        glfwPollEvents();

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        update();

        // Rendering
        ImGui::Render();
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, 1280, 720);
        glClearColor(0.5f,0.5f, 0.5f, 0.5f);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        // Update and Render additional Platform Windows
        // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
        //  For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
        {
            GLFWwindow* backup_current_context = glfwGetCurrentContext();
            ImGui::UpdatePlatformWindows();
            ImGui::RenderPlatformWindowsDefault();
            glfwMakeContextCurrent(backup_current_context);
        }

        glfwSwapBuffers(window);
    }


}

void AppWindow::addMouseCallBack(GLFWmousebuttonfun action)
{
    glfwSetMouseButtonCallback(window, action);
}

void AppWindow::addKeyCallBack(GLFWkeyfun action)
{
    glfwSetKeyCallback(window, action);
}

my main method:

int main(int ,  char**) {
AppWindow window("My app", 1280, 720);


window.run();

return 0;

}

AppWindow.h:

#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "imgui.h"

class AppWindow
{
public:
    ImVec4 BgColour = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
    ImGuiIO io;
    GLFWwindow* window;
    void (*updateFunction)() = nullptr;

    AppWindow(const char* title, int width, int height);
    ~AppWindow();


    void setUpdateFunction(void (*updateFunction)());

    void update();

    void run();

    void addMouseCallBack(GLFWmousebuttonfun action);

    void addKeyCallBack(GLFWkeyfun action);

here is an image of my folder structure: my folder structure

here is the imgui files I copied over: imgui files

my include directory contains: Desktop\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\imguiDependencies Desktop\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\Libraries\include

my library directory: Desktop\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\Libraries\lib

additonal dependencies: glfw3.lib

I figured out that this occurs due to a memory access violation in this portion of code as it runs:

glViewport(0, 0, 1280, 720);
glClearColor(0.5f,0.5f, 0.5f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);

commenting that out just gives me a non-crashing black screen.

I suspect that this may be due to me installing opengl and glfw and imgui incorrectly.

I also don't think that it is because my window variable is being freed, as i declared it in the class declaration.


Solution

  • Okay, so i had to simply write:

    gladLoadGL();
    

    after

    glfwMakeContextCurrent(window);
    

    I don't remember seeing it in tutorials, or maybe I wasn't paying attention. Anyhow, thanks to @G.M. for making the solution clear to me.