首页 > 解决方案 > SFML - 运行时检查失败 #2 - 变量周围的堆栈已损坏,每次运行程序时都会发生

问题描述

我正在使用 Visual Studio 2017,我已经设置了链接器,包括在调试和发布配置中。每次我尝试运行代码时,它都会向我抛出围绕各种变量的损坏堆栈的异常。无论是“设置”、“窗口”等。

我运行了两个代码,结果都相同:

#include<SFML/Graphics.hpp>
#include<iostream>

int main()
{

    sf::ContextSettings settings;

    settings.majorVersion = 3;
    settings.minorVersion = 3;
    settings.depthBits = 24;    
    settings.antialiasingLevel = 16;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Game", sf::Style::Default);

    window.setActive(true);    
    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        window.clear(sf::Color::Black);
        window.display();

        sf::Event ev;
        while (window.pollEvent(ev))
        {
            if (ev.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
                    window.close();
            }
        }
    }
    return 0;
}

第二个:

#include<iostream>
#include<SFML/Graphics.hpp>

int main()
{
    using namespace sf;
    RenderTexture texture;
    texture.create(800, 600);
    texture.clear(Color::Black);
    texture.display();
    texture.getTexture().copyToImage().saveToFile("D:\output\asd.bmp");

    return 0;
}

两个代码都抛出类似的异常,第一个程序抛出:

运行时检查失败 #2 - 变量“设置”周围的堆栈已损坏。

运行时检查失败 #2 - 变量“window”周围的堆栈已损坏。

第二个扔

运行时检查失败 #2 - 变量“纹理”周围的堆栈已损坏。

当我在调试器中按“继续”时,它会将我指向带有未处理异常的 gs_report:

Project2.exe 中 0x00A36859 处的未处理异常:堆栈 cookie 检测代码检测到基于堆栈的缓冲区溢出。

是代码有问题,还是我的设置有问题?

标签: c++exceptionopenglsfml

解决方案


推荐阅读