Steamworks 和 SFML 程序退出时出现访问冲突

Access violation on program exit with Steamworks and SFML

同时使用 Steamworks 和 SFML 时程序退出时抛出异常: Exception thrown at 0x00007FFA919D024E (ntdll.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x0000000000000010.

我已经将程序缩减到最基本的部分,但仍然遇到问题:

#include <SFML/Graphics.hpp>
#include <steam/steam_api.h>

int main()
{
    SteamAPI_Init();

    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Title", sf::Style::Close);

    while (window.isOpen())
    {
        sf::Event e;
        while (window.pollEvent(e))
        {
            switch (e.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                    break;
                }
            }
        }
    }

    SteamAPI_Shutdown();

    return 0;
}

调用堆栈如下:

事实证明,解决方案很简单,只需将 Steamworks API 初始化移动到创建 window.

之后
#include <SFML/Graphics.hpp>
#include <steam/steam_api.h>

int main()
{
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Title", sf::Style::Close);

    SteamAPI_Init();

    while (window.isOpen())
    {
        sf::Event e;
        while (window.pollEvent(e))
        {
            switch (e.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                    break;
                }
            }
        }
    }

    SteamAPI_Shutdown();

    return 0;
}