如何解决未处理的异常?(GLFW3 OpenGL)
How to resolve unhandled exception? (OpenGL, GLFW3)
异常显示:
Foundry.exe中0x00F52157的未处理异常:0xC0000005:访问冲突读取位置0x00000030.
并指向controls.cpp:
中的这一行glfwGetCursorPos(window, &xpos, &ypos);
控制代码在单独的文件Controls .cpp:
#include "stdafx.h"
#include <GLFW/glfw3.h>
extern GLFWwindow* window;
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include "controls.hpp"
glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;
glm::mat4 getViewMatrix(){
return ViewMatrix;
}
glm::mat4 getProjectionMatrix(){
return ProjectionMatrix;
}
glm::vec3 position = glm::vec3( 0, 0, 5 );
float horizontalAngle = 3.14f;
float verticalAngle = 0.0f;
float initialFoV = 45.0f;
float speed = 3.0f;
float mouseSpeed = 0.005f;
void computeMatricesFromInputs(){
static double lastTime = glfwGetTime();
double currentTime = glfwGetTime();
float deltaTime = float(currentTime - lastTime);
double xpos;
double ypos;
glfwGetCursorPos(window, &xpos, &ypos);
glfwSetCursorPos(window, 1280/2, 1024/2);
horizontalAngle += mouseSpeed * float (1280/2 - xpos );
verticalAngle += mouseSpeed * float (1024/2 - ypos );
glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);
glm::vec3 up = glm::cross( right, direction );
if (glfwGetKey( window, GLFW_KEY_UP || GLFW_KEY_W ) == GLFW_PRESS){
position += direction * deltaTime * speed;
}
if (glfwGetKey( window, GLFW_KEY_DOWN || GLFW_KEY_S ) == GLFW_PRESS){
position -= direction * deltaTime * speed;
}
if (glfwGetKey( window, GLFW_KEY_RIGHT || GLFW_KEY_D ) == GLFW_PRESS){
position += right * deltaTime * speed;
}
if (glfwGetKey( window, GLFW_KEY_LEFT || GLFW_KEY_A ) == GLFW_PRESS){
position -= right * deltaTime * speed;
}
float FoV = initialFoV;
ProjectionMatrix = glm::perspective(FoV, 5.0f / 4.0f, 0.1f, 100.0f);
ViewMatrix = glm::lookAt(position,position+direction,up);
lastTime = currentTime;
}
程序运行良好,不需要通过输入控件修改矩阵。
老实说,我不太了解底层编程和内存分配,所以这可能是原因。
这是内存访问冲突,而不是异常。
计算矩阵时,window
可能是坏指针。代码中的某个地方是glfwCreateWindow()
分配给window
的结果。是在计算矩阵之前完成的吗?这个结果是不是没有NULL
值?在你的代码中可能有glfwDestroyWindow(window);
。如果是这样,是在计算矩阵之后完成的吗?
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 在Visual Studio Community中使用glfw3 glew和opengl获取访问冲突异常
- 如何解决未处理的异常?(GLFW3 OpenGL)