SFML community forums

Help => System => Topic started by: madcat on April 07, 2015, 03:03:45 pm

Title: loading resource through a thread with SFML 2.2
Post by: madcat 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?
Title: Re: loading resource through a thread with SFML 2.2
Post by: Nexus 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 (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368)? 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.
Title: Re: loading resource through a thread with SFML 2.2
Post by: Hiura 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...  :-\

Title: Re: loading resource through a thread with SFML 2.2
Post by: Hiura 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