Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unhandled exception at cleanupUnsharedResources [SOLVED]  (Read 822 times)

0 Members and 1 Guest are viewing this topic.

joe5513

  • Newbie
  • *
  • Posts: 11
    • View Profile
Unhandled exception at cleanupUnsharedResources [SOLVED]
« on: March 15, 2023, 07:50:43 pm »
I'm using a sfml compiled from master branch.
And i get Access violation reading location exeption at program end:
//main.cpp
#include <SFML/Graphics.hpp>
#include <memory>

std::unique_ptr<sf::RenderWindow> w;

int main(int argc, char const* argv[])
{
    sf::Texture t;
    t.loadFromFile("texture.png");

    w = std::make_unique<sf::RenderWindow>(sf::VideoMode(sf::Vector2u(320, 240)), "12",
                         sf::Style::Default);

    return 0;
}

Callsatack:
vtm.exe!std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::pair<void (*const)(void *),void *>>>>::operator*() Line 148 (c:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\list:148)
vtm.exe!std::_List_iterator<std::_List_val<std::_List_simple_types<std::pair<void (*const)(void *),void *>>>>::operator*() Line 239 (c:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\list:239)
vtm.exe!sf::priv::GlContext::cleanupUnsharedResources() Line 753 (***\sfml\src\SFML\Window\GlContext.cpp:753)
vtm.exe!sf::priv::WglContext::~WglContext() Line 152 (***\sfml\src\SFML\Window\Win32\WglContext.cpp:152)
vtm.exe!sf::priv::WglContext::~WglContext() Line 150 (***\sfml\src\SFML\Window\Win32\WglContext.cpp:150)
vtm.exe!std::default_delete<sf::priv::WglContext>::operator()(sf::priv::WglContext * _Ptr) Line 3142 (c:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\memory:3142)
vtm.exe!std::unique_ptr<sf::priv::WglContext,std::default_delete<sf::priv::WglContext>>::~unique_ptr() Line 3254 (c:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\memory:3254)
vtm.exe!`anonymous namespace&#39;::GlContextImpl::`dynamic atexit destructor for &#39;sharedContext&#39;() Line 182 (***\sfml\src\SFML\Window\GlContext.cpp:182)
ucrtbased.dll!00007fff5a124957() (Unknown Source:0)
ucrtbased.dll!00007fff5a124365() (Unknown Source:0)
ucrtbased.dll!00007fff5a12449a() (Unknown Source:0)
ucrtbased.dll!00007fff5a124b01() (Unknown Source:0)
ucrtbased.dll!00007fff5a123cd1() (Unknown Source:0)
ucrtbased.dll!00007fff5a123b7d() (Unknown Source:0)
ucrtbased.dll!00007fff5a123bea() (Unknown Source:0)
ucrtbased.dll!00007fff5a123e64() (Unknown Source:0)
ucrtbased.dll!00007fff5a1241f6() (Unknown Source:0)
vtm.exe!__scrt_common_main_seh() Line 297 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:297)
vtm.exe!__scrt_common_main() Line 331 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331)
vtm.exe!mainCRTStartup(void * __formal) Line 17 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17)

Am i doing something stupid?

ADD: should i post an issue on github instead this forum?
« Last Edit: March 18, 2023, 05:36:01 pm by joe5513 »

joe5513

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unhandled exception at cleanupUnsharedResources
« Reply #1 on: March 17, 2023, 07:06:33 pm »
Release of unique_ptr after return from main tries to dereference end list iterator.
This seems like bug to me and can be reproduced with the code above..
Can someone confirm? :'(

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Unhandled exception at cleanupUnsharedResources
« Reply #2 on: March 18, 2023, 07:31:22 am »
It looks like w is global.
SFML resources being global often has issues. Maybe it's getting destroyed at the wrong time due to that.

Have you tried the exact same code but with the w pointer being created inside main?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Unhandled exception at cleanupUnsharedResources
« Reply #3 on: March 18, 2023, 01:38:25 pm »
The global destruction order is undefined, since SFML depends on some global states itself, it can happen that those dependencies are cleared before the window is destroyed, leading to errors.

I highly recommend to not use globals at all and especially not SFML resources.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

joe5513

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unhandled exception at cleanupUnsharedResources
« Reply #4 on: March 18, 2023, 05:35:25 pm »
The global destruction order is undefined (...)

This make sence, thanks.