From f5dc7e2c85436877606af46a011c2fba112185a9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 18 Oct 2020 06:54:18 +0200 Subject: Fix potential crash in thumbnail loader: 2 --- src/AsyncImageLoader.cpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'src/AsyncImageLoader.cpp') diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index fb864c2..8f771e7 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -70,16 +70,8 @@ namespace QuickMedia { return ""; } - // TODO: Remove and use std::thread and detach instead of std::async - AsyncImageLoader::~AsyncImageLoader() { - if(load_image_future.valid()) - load_image_future.get(); - } - // TODO: Run in 5 different threads, and add download_to_file(_cache) to reduce memory usage in each (use -O curl option) bool AsyncImageLoader::load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, bool use_tor, std::shared_ptr thumbnail_data) { - update(); - if(loading_image) return false; @@ -95,7 +87,8 @@ namespace QuickMedia { return true; } - load_image_future = std::async(std::launch::async, [url, local, resize_target_size, thumbnail_data, use_tor]() mutable { + // TODO: Keep the thread running and use conditional variable instead to sleep until a new image should be loaded. Same in ImageViewer. + load_image_thread = std::thread([this, url, local, resize_target_size, thumbnail_data, use_tor]() mutable { // TODO: Use sha256 instead of base64_url encoding Path thumbnail_path = get_cache_dir().join("thumbnails").join(base64_url::encode(url)); @@ -103,17 +96,20 @@ namespace QuickMedia { if(thumbnail_data->image->loadFromFile(thumbnail_path.data)) { fprintf(stderr, "Loaded %s from thumbnail cache\n", url.c_str()); thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; + loading_image = false; return; } else { if(local) { if(!thumbnail_data->image->loadFromFile(url)) { thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; + loading_image = false; return; } } else { std::string texture_data; if(download_to_string_cache(url, texture_data, {}, use_tor, true) != DownloadResult::OK || !thumbnail_data->image->loadFromMemory(texture_data.data(), texture_data.size())) { thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; + loading_image = false; return; } } @@ -127,21 +123,17 @@ namespace QuickMedia { thumbnail_data->image = std::move(destination_image); save_image_as_thumbnail_atomic(*thumbnail_data->image, thumbnail_path, get_ext(url)); thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; + loading_image = false; return; } } thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; + loading_image = false; return; }); + load_image_thread.detach(); return true; } - - void AsyncImageLoader::update() { - if(loading_image && load_image_future.valid() && load_image_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { - load_image_future.get(); - loading_image = false; - } - } } \ No newline at end of file -- cgit v1.2.3