loader: fixed potential memory leaks in font handling

Simply loading a font doesn't mean it is in use.
Fixed the loader's reference count to prevent incorrect increments.
This commit is contained in:
Hermet Park 2025-02-20 23:57:38 +09:00 committed by Hermet Park
parent a768276bb6
commit d50632d0d6
2 changed files with 6 additions and 3 deletions

View file

@ -249,7 +249,7 @@ bool LoaderMgr::term()
{ {
//clean up the remained font loaders which is globally used. //clean up the remained font loaders which is globally used.
INLIST_SAFE_FOREACH(_activeLoaders, loader) { INLIST_SAFE_FOREACH(_activeLoaders, loader) {
if (loader->type != FileType::Ttf) break; if (loader->type != FileType::Ttf) continue;
auto ret = loader->close(); auto ret = loader->close();
_activeLoaders.remove(loader); _activeLoaders.remove(loader);
if (ret) delete(loader); if (ret) delete(loader);

View file

@ -46,11 +46,14 @@ Result Text::load(const char* filename) noexcept
{ {
#ifdef THORVG_FILE_IO_SUPPORT #ifdef THORVG_FILE_IO_SUPPORT
bool invalid; //invalid path bool invalid; //invalid path
if (!LoaderMgr::loader(filename, &invalid)) { auto loader = LoaderMgr::loader(filename, &invalid);
if (loader) {
if (loader->sharing > 0) --loader->sharing; //font loading doesn't mean sharing.
return Result::Success;
} else {
if (invalid) return Result::InvalidArguments; if (invalid) return Result::InvalidArguments;
else return Result::NonSupport; else return Result::NonSupport;
} }
return Result::Success;
#else #else
TVGLOG("RENDERER", "FILE IO is disabled!"); TVGLOG("RENDERER", "FILE IO is disabled!");
return Result::NonSupport; return Result::NonSupport;