【发布时间】:2012-04-03 21:35:55
【问题描述】:
我正在尝试编译一个程序,该程序在我的台式机上编译得非常好,但在我的笔记本电脑上,它可以编译,但无论何时运行都会给我这个错误:
Windows 已在 RR.exe 中触发断点。
这可能是由于堆损坏,这表明存在错误 RR.exe 或它已加载的任何 DLL。
这也可能是由于用户在 RR.exe 获得焦点时按 F12。
输出窗口可能有更多的诊断信息。
我已经注释掉了行,直到找到导致错误的行:
if(glfwOpenWindow(width_, height_, 0, 0, 0, 0, 32, 0, GLFW_WINDOW) != GL_TRUE) {
throw std::runtime_error("Unable to open GLFW window");
}
奇怪的是,如果我用常量替换 width_ 和 height_,例如分别为 800 和 600,它会停止堆损坏。此外,如果我只使用构造函数设置的默认值而不是传递值,它不会崩溃。
这是完整的代码。以上几行在Window 构造函数中。
window.h
#pragma once
#include <iostream>
#include <GL\glew.h>
#include <GL\glfw.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "GLFW.lib")
class Window {
public:
Window(unsigned width = 800, unsigned height = 600);
~Window();
void clear();
inline void display() { glfwSwapBuffers(); }
inline bool exit() { return !glfwGetWindowParam(GLFW_OPENED); }
private:
unsigned width_, height_;
};
window.cpp
#include "window.h"
Window::Window(unsigned width, unsigned height) : width_(width), height_(height) {
if(glfwInit() != GL_TRUE) {
throw std::runtime_error("Unable to initialize GLFW");
}
if(glfwOpenWindow(width_, height_, 0, 0, 0, 0, 32, 0, GLFW_WINDOW) != GL_TRUE) { //crash
//if(glfwOpenWindow(800, 600, 0, 0, 0, 0, 32, 0, GLFW_WINDOW) != GL_TRUE) { //no crash
throw std::runtime_error("Unable to open GLFW window");
}
GLenum result = glewInit();
if(result != GLEW_OK) {
std::stringstream ss;
ss << "Unable to initialize glew: " << glewGetErrorString(result);
throw std::runtime_error(ss.str());
}
}
Window::~Window() {
glfwTerminate();
}
void Window::clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}
main.cpp
#include "window.h"
int main() {
Window wind(1024, 800); //crash
Window wind(800, 600); //crash
Window wind(); //works
return 0;
}
【问题讨论】:
-
+1 用于提供完整的、相对较短的测试用例。
-
您是否确保两台机器上的系统/运行时 DLL 版本完全相同?
-
它们是完全相同的 DLL、.libs 和项目,因为我将它们保存在 Dropbox 上。
-
窗风(); - 这是一个函数声明,而不是 Window 类型的默认构造变量
-
你确定 glfwOpenWindow 的行会导致崩溃,而不仅仅是暴露它吗? width_ 和 height_ 在调用时是否包含有效且正确的值?