From 0cf9f4bcd0697264f887fde7ce7117715e728b36 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 3 May 2018 20:06:20 +0200 Subject: Remove gif file data if gif fails to load --- src/Cache.cpp | 6 +++++- src/Gif.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index df97174..a4c740f 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -110,9 +110,10 @@ namespace dchat ImageByUrlResult loadImageFromFile(const boost::filesystem::path &filepath) { + StringView fileContent; try { - StringView fileContent = getFileContent(filepath); + fileContent = getFileContent(filepath); if(Gif::isDataGif(fileContent)) { Gif *gif = new Gif(move(fileContent)); @@ -124,17 +125,20 @@ namespace dchat if(texture->loadFromMemory(fileContent.data, fileContent.size)) { delete fileContent.data; + fileContent.data = nullptr; texture->setSmooth(true); texture->generateMipmap(); return { texture, ImageByUrlResult::Type::CACHED }; } delete texture; delete fileContent.data; + fileContent.data = nullptr; } } catch(std::exception &e) { fprintf(stderr, "Failed to load image %s, reason: %s\n", filepath.string().c_str(), e.what()); + delete fileContent.data; } return { (sf::Texture*)nullptr, ImageByUrlResult::Type::FAILED_DOWNLOAD }; } diff --git a/src/Gif.cpp b/src/Gif.cpp index 4d1295c..6b38f10 100644 --- a/src/Gif.cpp +++ b/src/Gif.cpp @@ -135,9 +135,11 @@ namespace dchat void Gif::draw(sf::RenderTarget &target) { double timeElapsedMilli = (double)frameTimer.getElapsedTime().asMilliseconds(); - // If gif is not redrawn for a while, then we want to reset it; otherwise the decoding loop will take too long time. - // This means that if gif is not visible for a while and then it becomes visible, the gif will reset instead of trying to process several seconds of frames - if(timeElapsedMilli > 3000.0) + // If gif is not redrawn for a while, then we reset timer (gif is paused). This happens when gif is not visible and then appears visible + // (because it's visible in window). The reason this is done is to prevent too much time between rendering gif frames, as processing a gif + // requires to process all frames between two points in time, if elapsed frame time is too high, then we would require to process several + // frames of gif in one application render frame. + if(timeElapsedMilli > 1000.0) timeElapsedMilli = 0.0; double frameDeltaCs = timeElapsedMilli * 0.1; // Centisecond frameTimer.restart(); -- cgit v1.2.3