Search code examples
c++sfmlsteamworks-api

Access violation on program exit with Steamworks and SFML


I get an exception thrown when the program exits when using Steamworks and SFML together: Exception thrown at 0x00007FFA919D024E (ntdll.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x0000000000000010.

I've cut back the program to its very basics while still getting the problem:

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

Here's the call stack: call stack


Solution

  • So it turns out the solution was as simple as moving the Steamworks API initialisation to after the creation of the 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;
    }