fix memory errors realted to cache

This commit is contained in:
arthur.jamet
2021-06-14 17:34:06 +02:00
parent 5ea5e048c9
commit d17836685e
9 changed files with 94 additions and 37 deletions

View File

@@ -73,24 +73,6 @@ namespace RAY {
template<>
class Cache<::ModelAnimation> {
public:
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
{
if (this->_cache.find(path) != this->_cache.end())
return this->_cache[path];
::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter);
unsigned int animCount = *counter;
this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>(
animations, [this, animCount](::ModelAnimation *p) {
this->_dataUnloader(p, animCount);
}));
return this->_cache[path];
};
private:
//! @brief function to call to load data
std::function<::ModelAnimation *(const char *, int *)> _dataLoader;
@@ -98,10 +80,34 @@ namespace RAY {
//! @brief function to call when the ray data will be unloaded
std::function<void(::ModelAnimation *, unsigned int)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::ModelAnimation>> _cache;
};
typedef struct {
std::shared_ptr<::ModelAnimation> animations;
int animationsCount;
} AnimationsHolder;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, AnimationsHolder> _cache;
public:
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
{
if (this->_cache.find(path) != this->_cache.end()) {
*counter = this->_cache[path].animationsCount;
} else {
::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter);
int animCount = *counter;
this->_cache.emplace(path, (AnimationsHolder){
std::shared_ptr<::ModelAnimation>(
animations, [this, animCount](::ModelAnimation *p) {
this->_dataUnloader(p, animCount);
}), animCount});
}
return this->_cache[path].animations;
};
};
template<>
class Cache<::Shader>
{