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

Author Topic: [Solved] Read Access Violation: "mutex" in GlContext.cpp  (Read 7054 times)

0 Members and 1 Guest are viewing this topic.

Qu32n

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Read Access Violation: "mutex" in GlContext.cpp
« on: December 21, 2018, 10:04:11 am »
Hello!  :)

I'm using SFML 2.5.1 for Visual C++ 15 (2017) - 32-bit

This bug only seems to happen in the following situations:
1) You're using STATIC linking
2) You declare an extern object that inherits from sf::GlContext

This is the code needed to reproduce the bug:
// Header.hpp
#pragma once
#include <SFML/Graphics.hpp>

// Exception in GlContext.cpp
// Line 245: pointer variable "(anonymous namespace)::mutex::m_mutexImpl" was NULL
extern sf::Texture UIButtonTexture;
 
// Header.cpp
#include "Header.h"
// Variable is never used.
sf::Texture UIButtonTexture;
 

As far as I know, sf::Mutex::Mutex() is never called in the program heap for some reason, therefor making Mutex::m_mutexImpl == nullptr.

This ultimately causes a read access violation in Mutex::lock()
// SFML/System/Mutex.cpp
void Mutex::lock()
{
    // Read violation; m_mutexImpl is nullptr
    // Note that it WILL call lock() but will fail when it tries to access
    // it's members because the 'this' pointer is nullptr.
    m_mutexImpl->lock();
}
 

I tried making my extern variable a pointer, but it resulted in the same error.

I'm not sure why sf::Mutex `anonymous-namespace'::mutex's constructor is never called.
But alas, I'm reporting this bug in the hopes that someone will figure out what went wrong!  ;D

Cheers!
« Last Edit: December 21, 2018, 11:00:52 am by Qu32n »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: [Bug] Read Access Violation: &quot;mutex&quot; in GlContext.cpp
« Reply #1 on: December 21, 2018, 10:14:22 am »
You're declaring and initializing a texture in global scope. The initialization order of global objects is undefined.

Simply put, you can't initialize SFML resources in global scope.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Qu32n

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [Bug] Read Access Violation: "mutex" in GlContext.cpp
« Reply #2 on: December 21, 2018, 11:00:25 am »
The initialization order of global objects is undefined.

Ah, that makes a lot of sense :P

Thanks for the help! Sorry for the dumb question :)
« Last Edit: December 21, 2018, 11:05:55 am by Qu32n »