glfwWindowShouldClose 错误 [已关闭]

glfwWindowShouldClose error [closed]

提问人:Rex 提问时间:8/22/2017 最后编辑:Rabbid76Rex 更新时间:8/22/2017 访问量:3478

问:


6年前关闭。

我目前正在使用 opengl 和 glfw 制作一个小型 c++ 引擎,在尝试调用该函数时遇到一个奇怪的错误,这是我的代码:glfwWindowShouldClose(window)

#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 文件:

#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

现在 Window.cpp 文件:

#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();
}

所以正如你所看到的,程序在运行时给了我一个错误,而不是在我编译时,错误是:

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

我假设没有创建查看的变量?glfwWindowShouldClose

如果您需要知道我在 Windows 10 上运行并且我使用 Visual Studio 2015

C++ OpenGL GLFW

评论


答:

2赞 derhass 8/22/2017 #1
if (glfwWindowShouldClose(window))//here is the error

那是因为 NULL 指针。window

您可以在此处初始化它:

GLFWwindow* window = 0;

并且永远不会再改变它的价值。

此功能

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

只是更新它是变量的本地副本,并在函数离开时泄漏指针。Window