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 Mira Grudzinska
parent 07da6c5870
commit 4ea95d8707
2 changed files with 10 additions and 2 deletions

View file

@ -267,7 +267,11 @@ bool LoaderMgr::term()
auto loader = _activeLoaders.head;
//clean up the remained font loaders which is globally used.
while (loader && loader->type == FileType::Ttf) {
while (loader) {
if (loader->type != FileType::Ttf) {
loader = loader->next;
continue;
}
auto ret = loader->close();
auto tmp = loader;
loader = loader->next;

View file

@ -62,7 +62,11 @@ Result Text::load(const std::string& path) noexcept
{
#ifdef THORVG_FILE_IO_SUPPORT
bool invalid; //invalid path
if (!LoaderMgr::loader(path, &invalid)) {
auto loader = LoaderMgr::loader(path, &invalid);
if (loader) {
if (loader->sharing > 0) --loader->sharing; //font loading doesn't mean sharing.
return Result::Success;
} else {
if (invalid) return Result::InvalidArguments;
else return Result::NonSupport;
}