【问题标题】:glfwWindowShouldClose error [closed]glfwWindowShouldClose 错误[关闭]
【发布时间】:2018-01-30 03:01:46
【问题描述】:

我目前正在使用 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


    【解决方案1】:
    if (glfwWindowShouldClose(window))//here is the error
    

    那是因为window 是一个 NULL 指针。

    你在这里初始化它:

    GLFWwindow* window = 0;
    

    永远不要再改变它的价值。

    这个函数

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

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

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 2014-09-18
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多