Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when adding a custom program which already exists #20550

Open
wants to merge 3 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cocos/renderer/backend/ProgramCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace
}

std::unordered_map<backend::ProgramType, backend::Program*> ProgramCache::_cachedPrograms;
std::unordered_map<std::string, backend::Program*> ProgramCache::_cachedCustomPrograms;

ProgramCache* ProgramCache::_sharedProgramCache = nullptr;

ProgramCache* ProgramCache::getInstance()
Expand Down Expand Up @@ -303,6 +305,36 @@ void ProgramCache::removeAllPrograms()
program.second->release();
}
_cachedPrograms.clear();

for (auto& program : _cachedCustomPrograms)
{
program.second->release();
}
_cachedCustomPrograms.clear();
}

void ProgramCache::addCustomProgram(const std::string &key, backend::Program *program)
{
const auto& iter = _cachedCustomPrograms.find(key);
if (_cachedCustomPrograms.end() != iter)
{
iter->second->release();
_cachedCustomPrograms.erase(iter);
}

_cachedCustomPrograms.emplace(key, program);
}

backend::Program* ProgramCache::getCustomProgram(const std::string &key) const
{
const auto& iter = ProgramCache::_cachedCustomPrograms.find(key);
if (ProgramCache::_cachedCustomPrograms.end() != iter)
{
return iter->second;
}

CCLOG("Warning: program %s not found in cache.", key.c_str());
return nullptr;
}

CC_BACKEND_END
11 changes: 11 additions & 0 deletions cocos/renderer/backend/ProgramCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class ProgramCache : public Ref
*/
void removeAllPrograms();

/**
* Add custom program to the cache.
* @param key Specifies the key used to store the program.
* @param program Specifies the program to store in the cache.
*/
void addCustomProgram(const std::string& key, backend::Program* program);

/// Get custom program from cache.
backend::Program* getCustomProgram(const std::string& key) const;

protected:
ProgramCache() = default;
virtual ~ProgramCache();
Expand All @@ -82,6 +92,7 @@ class ProgramCache : public Ref
void addProgram(ProgramType type);

static std::unordered_map<backend::ProgramType, backend::Program*> _cachedPrograms; ///< The cached program object.
static std::unordered_map<std::string, backend::Program*> _cachedCustomPrograms; ///< The cached custom program object.
static ProgramCache *_sharedProgramCache; ///< A shared instance of the program cache.
};

Expand Down