gpt4 book ai didi

c++ - 堆损坏但仅在笔记本电脑上编译时

转载 作者:可可西里 更新时间:2023-11-01 16:10:27 26 4
gpt4 key购买 nike

我正在尝试编译一个程序,该程序在我的台式机上编译得很好,但在我的笔记本电脑上,它可以编译,但无论何时运行都会给我这个错误:

Windows has triggered a breakpoint in RR.exe.

This may be due to a corruption of the heap, which indicates a bug in RR.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while RR.exe has focus.

The output window may have more diagnostic information.

我已经注释掉了行,直到我找到导致错误的行:

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;
}

最佳答案

问题似乎与 glfw 有关:

我假设,您正在尝试使用动态链接的 GLFW。 glfw header 中的注意事项:

#if defined(_WIN32) && defined(GLFW_BUILD_DLL)

/* We are building a Win32 DLL */
#define GLFWAPI __declspec(dllexport)
#define GLFWAPIENTRY __stdcall
#define GLFWCALL __stdcall
#elif defined(_WIN32) && defined(GLFW_DLL)

/* We are calling a Win32 DLL */
#if defined(__LCC__)
#define GLFWAPI extern
#else
#define GLFWAPI __declspec(dllimport)
#endif
#define GLFWAPIENTRY __stdcall
#define GLFWCALL __stdcall

#else

/* We are either building/calling a static lib or we are non-win32 */
#define GLFWAPIENTRY
#define GLFWAPI
#define GLFWCALL

#endif

GLFW_BUILD_DLL 显然是在构建dll时设置的,它定义了API函数与 __stdcall 调用转换。

但是在使用库时你还没有定义GLFW_DLL,所以你的代码假定了__cdecl调用转换。 _cdecl__stdcall 之间的区别通常是 caller function 应该首先清理堆栈,最后清理 callee案子。所以你清理了堆栈两次,这就是堆栈损坏的原因。

在您的程序中包含 glfw 之前,我定义了 GLFW_DLL 之后,它开始正常工作。另请注意,在定义 GLFW_DLL 之后,我使用了 mingw 并且必须针对 glfwdll.a 而不是 glfw.a 进行链接。

关于c++ - 堆损坏但仅在笔记本电脑上编译时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9769519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com