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

Author Topic: Access Violation Error after window.close()  (Read 362 times)

0 Members and 1 Guest are viewing this topic.

muhhae

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Access Violation Error after window.close()
« on: August 17, 2023, 03:01:15 am »
Idk what really happen but after. window.close() is called

https://github.com/muhhae/tetromino-sfml

it doesnt really affect gameplay. It just that everytime i close the app there's a short pause. The exception only showed when i use debugger.

this happen:
Exception thrown at 0x00007FF719A787EC in App.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Callstack:
App.exe!sf::priv::GlContext::acquireTransientContext() Line 411 (d:\vcpkg\buildtrees\sfml\src\2.6.0-2d6a42d05d.clean\src\SFML\Window\GlContext.cpp:411)
App.exe!sf::GlResource::TransientContextLock::TransientContextLock() Line 58 (d:\vcpkg\buildtrees\sfml\src\2.6.0-2d6a42d05d.clean\src\SFML\Window\GlResource.cpp:58)
App.exe!sf::Texture::~Texture() Line 117 (d:\vcpkg\buildtrees\sfml\src\2.6.0-2d6a42d05d.clean\src\SFML\Graphics\Texture.cpp:117)
App.exe!sf::Font::Page::~Page() (Unknown Source:0)
App.exe!std::pair<unsigned int const ,sf::Font::Page>::~pair<unsigned int const ,sf::Font::Page>() (Unknown Source:0)
App.exe!std::pair<unsigned int const ,sf::Font::Page>::`scalar deleting destructor'(unsigned int) (Unknown Source:0)
App.exe!std::_Default_allocator_traits<std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>>>::destroy<std::pair<unsigned int const ,sf::Font::Page>>(std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>> & __formal, std::pair<unsigned int const ,sf::Font::Page> * const _Ptr) Line 688 (d:\Other\VS\Visual Studio 2022 - Community\VC\Tools\MSVC\14.36.32532\include\xmemory:688)
App.exe!std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>::_Freenode<std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>>>(std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>> & _Al, std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *> * _Ptr) Line 383 (d:\Other\VS\Visual Studio 2022 - Community\VC\Tools\MSVC\14.36.32532\include\xtree:383)
App.exe!std::_Tree_val<std::_Tree_simple_types<std::pair<unsigned int const ,sf::Font::Page>>>::_Erase_tree<std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>>>(std::allocator<std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *>> & _Al, std::_Tree_node<std::pair<unsigned int const ,sf::Font::Page>,void *> * _Rootnode) Line 748 (d:\Other\VS\Visual Studio 2022 - Community\VC\Tools\MSVC\14.36.32532\include\xtree:748)
App.exe!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page>>,0>>::clear() Line 1358 (d:\Other\VS\Visual Studio 2022 - Community\VC\Tools\MSVC\14.36.32532\include\xtree:1358)
App.exe!sf::Font::cleanup() Line 566 (d:\vcpkg\buildtrees\sfml\src\2.6.0-2d6a42d05d.clean\src\SFML\Graphics\Font.cpp:566)
App.exe!sf::Font::~Font() Line 137 (d:\vcpkg\buildtrees\sfml\src\2.6.0-2d6a42d05d.clean\src\SFML\Graphics\Font.cpp:137)
App.exe!`dynamic atexit destructor for 'g_font''() (Unknown Source:0)
App.exe!_execute_onexit_table::__l2::<lambda>() Line 206 (d:\Other\VS\Microsoft VS Code\minkernel\crts\ucrt\src\appcrt\startup\onexit.cpp:206)
App.exe!__crt_seh_guarded_call<int>::operator()<void <lambda>(void),int <lambda>(void) &,void <lambda>(void)>(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, _execute_onexit_table::__l2::int <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 204 (d:\Other\VS\Microsoft VS Code\VCCRT\vcruntime\inc\internal_shared.h:204)
App.exe!__acrt_lock_and_call<int <lambda>(void)>(const __acrt_lock_id lock_id, _execute_onexit_table::__l2::int <lambda>(void) && action) Line 974 (d:\Other\VS\Microsoft VS Code\minkernel\crts\ucrt\inc\corecrt_internal.h:974)
App.exe!_execute_onexit_table(_onexit_table_t * table) Line 231 (d:\Other\VS\Microsoft VS Code\minkernel\crts\ucrt\src\appcrt\startup\onexit.cpp:231)
App.exe!common_exit::__l2::<lambda>() Line 221 (d:\Other\VS\Microsoft VS Code\minkernel\crts\ucrt\src\appcrt\startup\exit.cpp:221)
App.exe!__crt_seh_guarded_call<void>::operator()<void <lambda>(void),void <lambda>(void) &,void <lambda>(void)>(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, common_exit::__l2::void <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 224 (d:\Other\VS\Microsoft VS Code\VCCRT\vcruntime\inc\internal_shared.h:224)
App.exe!__acrt_lock_and_call<void <lambda>(void)>(const __acrt_lock_id lock_id, common_exit::__l2::void <lambda>(void) && action) Line 974 (d:\Other\VS\Microsoft VS Code\minkernel\crts\ucrt\inc\corecrt_internal.h:974)

this is code that I use for Button, I just called window.close() nothing else.
Button quitButton;
    quitButton.setButtonPosition(sf::Vector2f(0, 300))
            .setOrigin(sf::Vector2f(115, 40))
            .setButtonSize(sf::Vector2f(230, 80))
            .setButtonTexture(getTexture(buttonTexturePath))
           
            .setTextPosition(sf::Vector2f(0, 295))
            .setTextString("QUIT")
            .setTextSize(30)
            .setTextCenter()
            .setFont(g_font)
           
            .setButtonOnHover([&]()
            {
                quitButton.setTextPosition(sf::Vector2f(0, 300));
                quitButton.setButtonTexture(getTexture(buttonOnHoverTexturePath));
            })
            .setButtonOnUpdate([&]()
            {
                quitButton.setButtonTexture(getTexture(buttonTexturePath))
                          .setTextPosition(sf::Vector2f(0, 295));
            })
            .setButtonOnClick([&]()
            {
                window.close();
            });
 

any suggestion?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10693
    • View Profile
    • development blog
    • Email
Re: Access Violation Error after window.close()
« Reply #1 on: August 17, 2023, 08:31:46 pm »
Your font is a global variable. Destruction order is undefined in C++, which means that SFML's internal globals can get destroyed before your font global and thus bring down your application.

Don't use SFML resources in a global scope, to prevent such issues.
For resource management, I personally recommend using a ResourceHolder
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

muhhae

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Access Violation Error after window.close()
« Reply #2 on: August 18, 2023, 03:13:49 am »
Your font is a global variable. Destruction order is undefined in C++, which means that SFML's internal globals can get destroyed before your font global and thus bring down your application.

Don't use SFML resources in a global scope, to prevent such issues.
For resource management, I personally recommend using a ResourceHolder

Thanks, its fixed.