glfwWindowShouldClose error

-2

I am currently making a small c++ engine using opengl and glfw and I get a weird error while trying to call the glfwWindowShouldClose(window) function here is my code :

#include "Engine\Window.h"

#include <iostream>

using namespace std;

int main()
{
    GLFWwindow* window = 0;
    Window::InitGLFW();
    Window::CreateWindow(window,640,480,"1");

    while (true)
    {
        if (glfwWindowShouldClose(window))//here is the error
        {
            Window::DestroyWindow(window);
            Window::EndGLFW();
            return 0;
        }
    }
    system("pause");
    return 0;
}

Window.h File :

#ifndef ENGINE_WINDOW
#define ENGINE_WINDOW

#include <iostream>
#include <vector>

#include "GL\glew.h"
#include "GLFW\glfw3.h"

using namespace std;

class Window
{
public:


    static bool InitGLFW();
    static void EndGLFW();
    static bool CreateWindow(GLFWwindow* Window, int Width, int Height, char* Title);

    static void DestroyWindow(GLFWwindow* Window);

private:
    static void error_callback(int error, const char* description);
};



#endif

Now the Window.cpp file :

#include "Window.h"


void Window::error_callback(int error, const char* description)
{
    cout << "Error: %s\n" << description;
}

bool Window::CreateWindow(GLFWwindow* Window, int Width, int Height,char* Title)
{
    Window = glfwCreateWindow(Width, Height, Title, NULL, NULL);
    if (!Window)
    {
        cout << "Window or OpenGL context creation failed";
        return 1;
    }
    glfwMakeContextCurrent(Window);
    return 0;
}

bool Window::InitGLFW()
{
    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
    {
        cout << "glfw Initialization failed";
        return 1;
    }
}

void Window::DestroyWindow(GLFWwindow* Window)
{
    glfwDestroyWindow(Window);
}

void Window::EndGLFW()
{
    glfwTerminate();
}

So as you can see the the program gives me an error when its running not when I compile and the error is:

Unhandled exception to 0x56B34B9F (glfw3.dll) in Engine.exe: 0xC0000005: Access violation when reading location 0x00000014.

I assume the variable that glfwWindowShouldClose look at is not created?

if you need to know i am running on Windows 10 and I use Visual Studio 2015

c++
opengl
glfw
asked on Stack Overflow Aug 22, 2017 by Rex • edited Aug 22, 2017 by Rabbid76

1 Answer

1
if (glfwWindowShouldClose(window))//here is the error

That's because window is a NULL pointer.

You initialize it here:

GLFWwindow* window = 0;

and never change it's value again.

This function

bool Window::CreateWindow(GLFWwindow* Window, int Width, int Height,char* Title)
{
    Window = glfwCreateWindow(Width, Height, Title, NULL, NULL);
    [...]
}

just updates it's local copy of the Window variable and leaks the pointer when the function is left.

answered on Stack Overflow Aug 22, 2017 by derhass

User contributions licensed under CC BY-SA 3.0