Hello peeps,
I've been using SFML for quite some time now, and I have to say that I absolutely love it.
Currently I'm building a game engine + all sorts of tools for my future games, and I just threw together this little file downloader that can be used in combination with SFML resources'
loadFromMemory() function.
It all works fine, the image is displayed correctly, but when I close the application, it crashes with an
EXC_BAD_ACCESS error and shows me the assemly code for the destructor of the Texture and for some
releasePool() method. I cannot seem to figure out why this is happening. It works without any problems with
sf::Font and
sf::SoundBuffer. Please help.
Download.cpp
Download::Download(const URL& url) :
m_data(),
m_url(url),
m_size(0),
m_state(Stopped),
m_function([](bool, const std::string&) {}){
fetchMetadata();
}
Download::~Download() {
m_thread.join();
}
void Download::onComplete(std::function<void (bool success, const std::string&)> function) {
m_function = function;
}
void Download::start() {
m_state = Downloading;
m_thread = std::thread(&Download::run, this);
}
int Download::getSize() const {
return m_size;
}
Download::State Download::getState() const {
return m_state;
}
bool Download::isComplete() const {
return m_state == Complete;
}
void Download::fetchMetadata() {
sf::Http http(m_url.getProtocol() + "://" + m_url.getHost());
sf::Http::Request request(m_url.getPath(), sf::Http::Request::Head);
sf::Http::Response response = http.sendRequest(request);
if (response.getStatus() == sf::Http::Response::Ok) {
m_size = std::stoi(response.getField("Content-Length"));
}
}
void Download::run() {
sf::Http http(m_url.getProtocol() + "://" + m_url.getHost());
sf::Http::Request request(m_url.getPath(), sf::Http::Request::Get);
sf::Http::Response response = http.sendRequest(request);
bool success;
if (response.getStatus() == sf::Http::Response::Ok) {
m_state = Complete;
m_data = response.getBody();
success = true;
} else {
m_state = Error;
success = false;
}
if (m_function) {
m_function(success, m_data);
}
}
main.cpp
int main(int argc, const char * argv[]) {
sf::Texture texture;
sf::Sprite sprite;
URL url("http://r.ddmcdn.com/s_f/o_1/cx_633/cy_0/cw_1725/ch_1725/w_720/APL/uploads/2014/11/too-cute-doggone-it-video-playlist.jpg");
Download download(url);
download.onComplete([&](bool success, const std::string& data) {
texture.loadFromMemory(data.c_str(), data.length());
sprite.setTexture(texture);
});
download.start();
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Downloading...");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color(60, 60, 60));
window.draw(sprite);
window.display();
}
return 0;
}
If I replace
sf::Texture and
sf::Sprite with
sf::Font and
sf::Text respectively (and change the URL obviously), there is no problem at all.