Vector error in Microsoft Visual C++

Started by _Mir_.cpp, 12 April 2025, 19:54:03

_Mir_.cpp

Hello! I have a problem. I followed installation tutorial for static linking and I added path to SFML 3.0.0 (I use SFML Graphics). I also added opengl32.lib, winmm.lib and freetype.lib to Linker->Input->Additional Dependencies because without these libs I had lots of errors during the build. Now, when I try to start my program with Debug configuration, I have an error:

File: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\vector
Line: 1750
Expression: vector erase iterator outside range.

I think it is internal error in TGUI or SFML because my code is very small and it doesn't use Vector. Tell me please how do I fix it.

texus

When it crashes, you should be able to get a call stack in Visual Studio.
That will tell you on which line in your code it crashes. I may be able to help you further if you can share the call stack.

Are you maybe mixing release and debug libraries (e.g. linking to libraries ending at "-d" in release mode)? That's could cause weird unexplainable crashes.

_Mir_.cpp

#2
My dependencies are correct (I checked). Call stack:
    FileScanner.exe!sf::priv::JoystickImpl::deviceObjectEnumerationCallback(struct DIDEVICEOBJECTINSTANCEW const *,void *)   
    FileScanner.exe!sf::priv::JoystickImpl::updateConnectionsDInput(void)   
    FileScanner.exe!sf::priv::JoystickImpl::updateConnections(void)   
    FileScanner.exe!sf::priv::JoystickImpl::initialize(void)   
    FileScanner.exe!sf::priv::JoystickManager::JoystickManager(void)   
    FileScanner.exe!sf::priv::JoystickManager::getInstance(void)   
    FileScanner.exe!sf::priv::WindowImpl::WindowImpl(void)   
    FileScanner.exe!sf::priv::WindowImplWin32::WindowImplWin32(class sf::VideoMode,class sf::String const &,unsigned int,enum sf::State,struct sf::ContextSettings const &)   
    FileScanner.exe!std::make_unique<class sf::priv::WindowImplWin32,class sf::VideoMode &,class sf::String const &,unsigned int &,enum sf::State &,struct sf::ContextSettings const &,0>(class sf::VideoMode &,class sf::String const &,unsigned int &,enum sf::State &,struct sf::ContextSettings const &)   
    FileScanner.exe!sf::priv::WindowImpl::create(class sf::VideoMode,class sf::String const &,unsigned int,enum sf::State,struct sf::ContextSettings const &)   
    FileScanner.exe!sf::Window::create(class sf::VideoMode,class sf::String const &,unsigned int,enum sf::State,struct sf::ContextSettings const &)
    FileScanner.exe!sf::RenderWindow::RenderWindow(class sf::VideoMode,class sf::String const &,unsigned int,enum sf::State,struct sf::ContextSettings const &)   
>   FileScanner.exe!`dynamic initializer for 'window''() Line 76   C++
    ucrtbased.dll!00007ffe0aba22a3()   Нет данных
    FileScanner.exe!__scrt_common_main_seh() Line 258   C++
    FileScanner.exe!__scrt_common_main() Line 331   C++
    FileScanner.exe!WinMainCRTStartup(void * __formal) Line 17   C++
    kernel32.dll!00007ffef608e8d7()   
    ntdll.dll!00007ffef6cdbf6c()

texus

#3
Do you have any joysticks connected? If so, does it help when they aren't connected?

The callstack looks a bit weird though. The deviceObjectEnumerationCallback isn't called from updateConnectionsDInput. The function also doesn't use a vector. So at least the top entry in the call stack seems like it might be corrupted. Could you copy the "sfml-*.pdb" files from SFML next to your exe? If you downloaded the SFML libraries then you can find these .pdb in the lib/Debug folder, otherwise they were created where the .lib files were created. With those pdb files the call stack should contain line numbers, which would give a more accurate point in the SFML code where the error occurs.

The error happens while creating the SFML window, so this doesn't seem to be related to TGUI. Do you get the same error when you don't link TGUI in your project? If the issue remains when removing TGUI (which I don't think is likely) then it would point to some compatibility issue with the linked TGUI and SFML libraries. If the issue remains then this is clearly an issue with SFML. Since I've never seen a crash at that location, you may want to ask the SFML team about it (e.g. on their Discord) and share the callstack and error message with them, maybe they can make some sense of it.

_Mir_.cpp

Hello.
1. I didn't use joysticks.
2. I can send here my .pdb file. Maybe it will be useful. Google Drive

texus

The pdb file itself isn't useful for me.
In the callstack you can see there are line number for the bottom lines because those are from your own program (e.g. line 76 was where you created the window). Those line numbers are known because you have that Filescanner.pdb file.
There exist pdb files for your SFML libraries as well. If you copy those files to the same place where you found your Filescanner.pdb then the callstack will show line numbers for all those lines in the callstack that came from inside the SFML library. So by including the pdb files from SFML into your project you would get a more detailed call stack (right now the callstack shows a crash inside the sf::JoystickImpl::updateConnectionsDInput function, but it doesn't exactly specify at which line of that function).

If you can bundle your entire project into a single zip file (including SFML and TGUI folders), so that it can be build on a computer without needing to download or install anything else (except Visual Studio, that is already installed), then I'm willing to see if I get the same issue when running it here or not. If I can reproduce it here then I should be able to fix it.

_Mir_.cpp

There are my project and my libraries. Libraries must be situated in folder: C:/Libraries. I hope it will be successful.

texus

I reproduced the crash here. Your mistake is that you are creating a global sf::RenderWindow.
You should never create SFML or TGUI objects globally. Having global pointers to them is fine, but you should always make certain that the constructors are executed after the WinMain function begins and destructors are executed before the WinMain function exits.

I moved the following two lines from global scope to the top of the WinMain function:
Code (cpp) Select
sf::RenderWindow window(sf::VideoMode(sf::Vector2u(800, 600)), "Disk Manager", sf::State::Windowed, sf::ContextSettings());
tgui::Gui gui(window);
I then had to replace some "gui" references inside the setupWidgets function to "ui". After that your program compiled and started fine.