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

Author Topic: Issue with Font when multiple threads are introduced  (Read 1716 times)

0 Members and 1 Guest are viewing this topic.

jace10

  • Newbie
  • *
  • Posts: 11
    • View Profile
Issue with Font when multiple threads are introduced
« on: May 11, 2020, 12:42:27 am »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Issue with Font when multiple threads are introduced
« Reply #1 on: May 11, 2020, 08:39:21 am »
Can you show more details (code!) about the thread?
Laurent Gomila - SFML developer

jace10

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Issue with Font when multiple threads are introduced
« Reply #2 on: May 12, 2020, 12:55:33 am »
Here is where the thread is created and joined:

void loadEvents()
{
  EventHandler::getInstance();
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
  //init logger
  loguru::add_file("everything.log", loguru::Append, loguru::Verbosity_MAX);

  //init RNG
  srand(static_cast<unsigned int>(time(NULL)));

  //loadEvents();
  thread eventLoadingThread(loadEvents);
  eventLoadingThread.join();
  //eventLoadingThread.detach();

  //instantiate scene classes
  MainMenu mainMenu = MainMenu();
  BattleScene battleScene = BattleScene();
  MapScene mapScene = MapScene();
  SceneManager sceneManager = SceneManager(&mainMenu, &battleScene, &mapScene);

  //main window loop
  WindowManager::getInstance()->run();
 
 
  return 0;
}
 

this is in my Main.cpp. EventHandler::getInstance triggers EventHandler to initialize its large array of objects, some of which include SFML entities, but no Font calls are made.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Issue with Font when multiple threads are introduced
« Reply #3 on: May 15, 2020, 11:26:30 am »
It's hard to tell really. Try using your debugger to check if everything was initialized properly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jace10

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Issue with Font when multiple threads are introduced
« Reply #4 on: May 15, 2020, 09:13:23 pm »
From what I can tell, the sf::Font class itself looks identical whether im running with the thread in the beginning or not, what else could I be looking at in the SFML area to ensure I initialized it correctly?

 

anything