From d0f18e741dba33ccc3dea268268f1905434b15f4 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 17 Oct 2024 11:15:56 +0900 Subject: [PATCH] lottie: release memory immediately after loading is complete. there is no need to retain the data, as the Lottie source is not reusable and is affected by the parsing mechanism. issue: https://github.com/thorvg/thorvg/issues/2647 --- src/loaders/lottie/tvgLottieLoader.cpp | 22 +++++++++++++++++----- src/loaders/lottie/tvgLottieLoader.h | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index 2e1c5513..a7f3bad2 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -44,11 +44,24 @@ void LottieLoader::run(unsigned tid) comp = parser.comp; } builder->build(comp); + + release(); } rebuild = false; } +void LottieLoader::release() +{ + if (copy) { + free((char*)content); + content = nullptr; + } + free(dirName); + dirName = nullptr; +} + + /************************************************************************/ /* External Class Implementation */ /************************************************************************/ @@ -63,8 +76,7 @@ LottieLoader::~LottieLoader() { done(); - if (copy) free((char*)content); - free(dirName); + release(); //TODO: correct position? delete(comp); @@ -76,6 +88,7 @@ bool LottieLoader::header() { //A single thread doesn't need to perform intensive tasks. if (TaskScheduler::threads() == 0) { + LoadModule::read(); run(0); if (comp) { w = static_cast(comp->w); @@ -87,7 +100,6 @@ bool LottieLoader::header() } else { return false; } - LoadModule::read(); } //Quickly validate the given Lottie file without parsing in order to get the animation info. @@ -253,11 +265,11 @@ bool LottieLoader::resize(Paint* paint, float w, float h) bool LottieLoader::read() { - if (!content || size == 0) return false; - //the loading has been already completed if (!LoadModule::read()) return true; + if (!content || size == 0) return false; + TaskScheduler::request(this); return true; diff --git a/src/loaders/lottie/tvgLottieLoader.h b/src/loaders/lottie/tvgLottieLoader.h index 1c4068b7..4e6b30a1 100644 --- a/src/loaders/lottie/tvgLottieLoader.h +++ b/src/loaders/lottie/tvgLottieLoader.h @@ -77,6 +77,7 @@ private: void clear(); float startFrame(); void run(unsigned tid) override; + void release(); };