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

Author Topic: loading resource through a thread with SFML 2.2  (Read 4046 times)

0 Members and 1 Guest are viewing this topic.

madcat

  • Newbie
  • *
  • Posts: 21
  • Creator of TacWars
    • View Profile
    • TacWars
loading resource through a thread with SFML 2.2
« on: April 07, 2015, 03:03:45 pm »
Hi, after upgrading to SFML 2.2 - I recompiled my code (with Xcode on OSX 10.9) and suddenly get a BAD ACCESS when the program exits.  :(

Based on Nexus' comments, I put here the minimal code to generate the problem (the result I get is attached below)
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/Lock.hpp>
#include <SFML/System/Mutex.hpp>
sf::Mutex gMutex;

//--- threaded F()
void tF(sf::Texture* apTex)
{ sf::Lock lock(gMutex);
  apTex->loadFromFile("icon.png");
}


//=== MAIN
int main(int, char const**)
{
  sf::Texture texture;
 
  sf::Thread thread(&tF, &texture);
  thread.launch();
 
  thread.wait();
 
  return 0;
}

It worked fine with SFML 2.1
Am I doing something wrong?
« Last Edit: April 08, 2015, 12:30:49 pm by madcat »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: loading resource through a thread with SFML 2.2
« Reply #1 on: April 07, 2015, 05:11:03 pm »
The mutex is not used, thus the application is not thread-safe.

But to show your problem, can you reduce the code to a minimal complete example? Get rid of all the unneeded things like conditional compilation, Apple-specific code, rendering, maybe event handling... Everything that doesn't directly contribute to the problem. If creating a texture is enough to trigger the error, then show only that code.
« Last Edit: April 07, 2015, 05:13:12 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: loading resource through a thread with SFML 2.2
« Reply #2 on: April 08, 2015, 10:28:30 am »
The code you posted didn't compiled as-is. You're missing an include for sf::Lock.

Anyway, that's not the issue here. It's more related to this one: https://github.com/SFML/SFML/issues/790

However, the simple workaround proposed in the issue description doesn't quite work for you...  :-\

SFML / OS X developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: loading resource through a thread with SFML 2.2
« Reply #3 on: September 08, 2015, 10:59:38 am »
Patch available: could you head over https://github.com/SFML/SFML/pull/962 and give it a try? Thanks
SFML / OS X developer