glfwWindowShouldClose error

glfwWindowShouldClose error

本文关键字:error glfwWindowShouldClose      更新时间:2023-10-16

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

#include "EngineWindow.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;
}

窗口.h 文件 :

#ifndef ENGINE_WINDOW
#define ENGINE_WINDOW
#include <iostream>
#include <vector>
#include "GLglew.h"
#include "GLFWglfw3.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

现在窗口.cpp文件:

#include "Window.h"

void Window::error_callback(int error, const char* description)
{
cout << "Error: %sn" << 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

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

这是因为window是一个空指针。

您可以在此处初始化它:

GLFWwindow* window = 0;

,再也不会改变它的价值。

此函数

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

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

相关文章: