Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSVC + vcpkg: Exception thrown. g was nullptr. #315

Open
pascal754 opened this issue Mar 3, 2025 · 11 comments
Open

MSVC + vcpkg: Exception thrown. g was nullptr. #315

pascal754 opened this issue Mar 3, 2025 · 11 comments

Comments

@pascal754
Copy link

Hi, the following code used to work using SFML 2.6 but an exception was thrown after updating to v3.0.

Image

OS: Windows 11
Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.14.0 Preview 1.1
> vcpkg version
vcpkg package management program version 2025-02-11-bec4296bf5289dc9ce83b4f5095943e44162f9c2
imgui-sfml:x64-windows                            3.0     
imgui:x64-windows                                 1.91.8#4
sfml:x64-windows                                  3.0.0
// main.cpp
// https://github.com/SFML/imgui-sfml/blob/master/examples/multiple_windows/main.cpp

#include "imgui.h" // necessary for ImGui::*, imgui-SFML.h doesn't include imgui.h

#include "imgui-SFML.h" // for ImGui::SFML::* functions and SFML-specific overloads

#include <SFML/Graphics.hpp>

int main()
{
  sf::RenderWindow window(sf::VideoMode({ 1280, 720 }), "ImGui + SFML = <3");
  window.setFramerateLimit(60);
  if (!ImGui::SFML::Init(window))
    return -1;

  sf::RenderWindow childWindow(sf::VideoMode({ 640, 480 }), "ImGui-SFML Child window");
  childWindow.setFramerateLimit(60);
  if (!ImGui::SFML::Init(childWindow))
    return -1;

  sf::Clock deltaClock;
  while (window.isOpen())
  {
    // Main window event processing
    while (const auto event = window.pollEvent())
    {
      ImGui::SFML::ProcessEvent(window, *event);
      if (event->is<sf::Event::Closed>())
      {
        if (childWindow.isOpen())
        {
          childWindow.close();
        }
        window.close();
        ImGui::SFML::Shutdown(); // will shutdown all windows
        return 0;                // return here so that we don't call Update/Render
      }
    }

    // Child window event processing
    if (childWindow.isOpen())
    {
      while (const auto event = childWindow.pollEvent())
      {
        ImGui::SFML::ProcessEvent(childWindow, *event);
        if (event->is<sf::Event::Closed>())
        {
          childWindow.close();
          ImGui::SFML::Shutdown(childWindow);
        }
      }
    }

    // Update
    const sf::Time dt = deltaClock.restart();
    ImGui::SFML::Update(window, dt);
    if (childWindow.isOpen())
    {
      ImGui::SFML::Update(childWindow, dt);
    }

    // Add ImGui widgets in the first window
    ImGui::SFML::SetCurrentWindow(window);
    ImGui::Begin("Hello, world!");
    ImGui::Button("Look at this pretty button");
    ImGui::End();
    ImGui::ShowDemoWindow();
    // Add ImGui widgets in the child window
    if (childWindow.isOpen())
    {
      ImGui::SFML::SetCurrentWindow(childWindow);
      ImGui::Begin("Works in a second window!");
      ImGui::Button("Example button");
      ImGui::End();
    }

    // Main window drawing
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    window.clear();
    window.draw(shape);
    ImGui::SFML::Render(window);
    window.display();

    // Child window drawing
    if (childWindow.isOpen())
    {
      sf::CircleShape shape2(50.f);
      shape2.setFillColor(sf::Color::Red);

      childWindow.clear();
      childWindow.draw(shape2);
      ImGui::SFML::Render(childWindow);
      childWindow.display();
    }
  }
}
@N1NJ4
Copy link

N1NJ4 commented Mar 7, 2025

Same/Similar; first call works (ImGui::SFML::Update()), second call (any ImGui::Begin()) its effectively nullptr.

@pascal754
Copy link
Author

If I add imgui-SFML.cpp to the project, the program runs without an error.

@ChrisThrasher
Copy link
Member

If I add imgui-SFML.cpp to the project, the program runs without an error.

Could you elaborate? Did you add imgui-SFML.cpp and correspondingly remove the imgui-SFML Vcpkg package?

@N1NJ4
Copy link

N1NJ4 commented Mar 8, 2025

If I add imgui-SFML.cpp to the project, the program runs without an error.

Could you elaborate? Did you add imgui-SFML.cpp and correspondingly remove the imgui-SFML Vcpkg package?

Edited: He Isn't wrong.., I have the source unpacked in a new folder in my vcpkg folder, so I just #include'd the .cpp file to my main.cpp (C:\vcpkg\source\imgui-sfml\imgui-SFML.cpp) (in my test app), removing the #include "imgui-SFML.h" that I had instead. changed nothing of my vcpkg configuration, and it works, trying my main program...

Edit(new): Working.

@pascal754
Copy link
Author

What I have observed is as follows.

  • Environment: MSVC (IDE) and vcpkg integration mode.
    I used this method when I posted the issue.
    I copied imgui-SFML.cpp from https://github.com/SFML/imgui-sfml/releases/tag/v3.0 and the programs runs.
    Image

  • In addition, I tried cmake + vcpkg.
    The same nullptr exception was thrown. However, if I add imgui-SFML.cpp to the project the program runs.

cmake_minimum_required(VERSION 3.26)

set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")

# imgui sfml example
project(ex CXX)

set(CMAKE_CXX_STANDARD 23)

find_package(imgui CONFIG REQUIRED)
find_package(ImGui-SFML CONFIG REQUIRED)
find_package(SFML COMPONENTS Graphics CONFIG REQUIRED)

add_executable(${PROJECT_NAME} main.cpp imgui-SFML.cpp)

target_link_libraries(
  ${PROJECT_NAME}
  PRIVATE
    ImGui-SFML::ImGui-SFML
)
  • In two cases above, vcpkg uses dll files.

  • Finally, I tried cmake fetch_content method and this works. I found out that this method uses static libraries. So, when dll files are used the nullptr exception occurs.
    cmake_fetch.zip

@pascal754
Copy link
Author

FYI, another vcpkg issue on Windows was resolved so you should update vcpkg first.
microsoft/vcpkg#44123

imgui-sfml:x64-windows                            3.0#1

@ChrisThrasher
Copy link
Member

FYI, another vcpkg issue on Windows was resolved so you should update vcpkg first. microsoft/vcpkg#44123

imgui-sfml:x64-windows                            3.0#1

I took a closer look at that and it motivated #316 but it doesn't seem related to this issue.

@ChrisThrasher
Copy link
Member

ChrisThrasher commented Mar 9, 2025

  • In two cases above, vcpkg uses dll files.
  • Finally, I tried cmake fetch_content method and this works. I found out that this method uses static libraries. So, when dll files are used the nullptr exception occurs.
    cmake_fetch.zip

So it's safe to say this error necessarily requires using DLLs? I wonder if it's something incorrect about how we're handling global state. I know that when switching from static libs to dynamic libs problems like this can arise if you're not careful. I wonder if the same problem exists other OSes when using dynamic libs.

@pascal754
Copy link
Author

My observation is end user's perspective.

vcpkg uses shared libs on Windows and static libs on Linux by default.
microsoft/vcpkg#19127 (comment)

I tested with the example code (https://github.com/SFML/imgui-sfml/tree/master/examples) using the latest version of libs. So, I don't understand "...problems like this can arise if you're not careful...".

My question is that why this happens when I updated libs to the latest version.

@ChrisThrasher
Copy link
Member

ChrisThrasher commented Mar 10, 2025

My comment wasn’t super clear. The “you” I’m referring to are the library maintainers such as myself who are putting globals in libraries. We have to be careful.

Are you able to test with shared libs on Linux to see if the problem also exists with that config?

@pascal754
Copy link
Author

The code runs using shared libs on Linux.

I followed instructions from this (microsoft/vcpkg#24048 (comment)) and tested on Fedora.

imgui:x64-linux-dynamic                           1.91.8#4
imgui-sfml:x64-linux-dynamic                      3.0#1
sfml:x64-linux-dynamic                            3.0.0

cmake -GNinja -DCMAKE_CXX_COMPILER=g++ -DVCPKG_TARGET_TRIPLET=x64-linux-dynamic -DCMAKE_BUILD_TYPE=Debug -B Debug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants