Hello,
I am writing a program, everything was going very smoothly with SMFL until I started trying to add some threads. I ran into an issue that causes a slight 5 second delay, (initializing/filling large array), so I wanted to spin off a thread at startup to get it out of the way early. However once I introduce the thread, my program always gets a "Read Access Violation" when it tries to call getGlobalBounds on one of my sf::Text objects later on, full call stack looks like this:
redBeard.exe!sf::Texture::update(const sf::Texture & texture, unsigned int x, unsigned int y) Line 478 C++
redBeard.exe!sf::Texture::update(const sf::Texture & texture) Line 443 C++
redBeard.exe!sf::Font::findGlyphRect(sf::Font::Page & page, unsigned int width, unsigned int height) Line 732 C++
redBeard.exe!sf::Font::loadGlyph(unsigned int codePoint, unsigned int characterSize, bool bold, float outlineThickness) Line 613 C++
redBeard.exe!sf::Font::getGlyph(unsigned int codePoint, unsigned int characterSize, bool bold, float outlineThickness) Line 363 C++
redBeard.exe!sf::Text::ensureGeometryUpdate() Line 519 C++
redBeard.exe!sf::Text::getLocalBounds() Line 362 C++
redBeard.exe!sf::Text::getGlobalBounds() Line 369 C++
> redBeard.exe!Button::displayText() Line 53 C++
redBeard.exe!Button::addDrawables() Line 150 C++
redBeard.exe!MainMenu::setup() Line 84 C++
redBeard.exe!SceneManager::init() Line 83 C++
redBeard.exe!SceneManager::SceneManager(MainMenu * mainMenu, BattleScene * battleScene, MapScene * mapScene) Line 12 C++
redBeard.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 49 C++
To be clear, right now I am joining on the thread, I see the same behavior whether I join it or detach it. The crash is occuring from my main thread, after the loading thread has completed (I know this because of the join).
I thought that perhaps if the other thread was calling loadFromFile on a font, it might be messing it up, but I have verified through debugging that my helper thread is calling no Font functions.
Here is what the full function that is crashing looks like:
void Button::displayText()
{
std::lock_guard<std::mutex> lck{ buttonSfMutex };
if (StringDb::StringEnum::NUM_STRINGS != textIndex)
{
std::string textContents = StringDb::getString(textIndex);
*textLabel = sf::Text();
font = sf::Font();
font.loadFromFile(StringDb::fontFile);
textLabel->setFont(font);
textLabel->setString(textContents);
textLabel->setCharacterSize(textSize);
if (true == active)
{
textLabel->setFillColor(activeTextColor);
}
else
{
textLabel->setFillColor(inactiveTextColor);
}
sf::FloatRect textBounds = textLabel->getGlobalBounds();
if (textBounds.width <= width && textBounds.height <= height)
{
//center text within button
textLabel->setPosition(x + ((width - textBounds.width) / 2), y + ((height - textBounds.height) / 4));
} //else, dont bother being smart about where its goin
else
{
textLabel->setPosition(x, y);
}
}
}
The font variable is declared like so in Button.h:
protected:
sf::Font font;
If I comment out the thread from running, everything works fine, so I think it has to have something with that, but I don't understand what could be going wrong since the font is local to the button and is being loaded in this function.
I appreciate any ideas, please let me know if you need to see more code or explanation on how I am doing something upstream.