Material: Fix several memory leaks caused by cyclic references

The class MaterialLibrary has a shared pointer to its Material objects and the class Material has a shared pointer to the MaterialLibrary.
The class MaterialManager owns all MaterialLibrary and Material objects in static containers. To resolve the cyclic references the method
cleanup() has been added.

The class ModelLibrary has a shared pointer to its Model objects and the class Model has a shared pointer to the ModelLibrary.
The class ModelManager owns all ModelLibrary and Model objects in static containers. To resolve the cyclic references the method
cleanup() has been added.

The Materials module registers a function to App::CleanupProcess that calls the above cleanup() methods.
NOTE: This registration is only done in debug mode mainly to satisfy memory checkers
This commit is contained in:
wmayer
2024-04-25 15:01:21 +02:00
parent 70950692eb
commit 451c55b41d
5 changed files with 41 additions and 0 deletions

View File

@@ -27,7 +27,10 @@
#include <Base/Interpreter.h>
#include <Base/PyObjectBase.h>
#include <App/CleanupProcess.h>
#include "MaterialFilterPy.h"
#include "MaterialLoader.h"
#include "MaterialManagerPy.h"
#include "MaterialPy.h"
#include "ModelManagerPy.h"
@@ -61,6 +64,12 @@ PyObject* initModule()
PyMOD_INIT_FUNC(Materials)
{
#ifdef FC_DEBUG
App::CleanupProcess::registerCleanup([](){
Materials::MaterialManager::cleanup();
Materials::ModelManager::cleanup();
});
#endif
PyObject* module = Materials::initModule();
Base::Console().Log("Loading Material module... done\n");

View File

@@ -77,6 +77,21 @@ void MaterialManager::initLibraries()
}
}
void MaterialManager::cleanup()
{
if (_libraryList) {
_libraryList->clear();
}
if (_materialMap) {
for (auto& it : *_materialMap) {
// This is needed to resolve cyclic dependencies
it.second->setLibrary(nullptr);
}
_materialMap->clear();
}
}
void MaterialManager::saveMaterial(const std::shared_ptr<MaterialLibrary>& library,
const std::shared_ptr<Material>& material,
const QString& path,

View File

@@ -54,6 +54,7 @@ public:
MaterialManager();
~MaterialManager() override = default;
static void cleanup();
static std::shared_ptr<Material> defaultMaterial();
static QString defaultMaterialUUID();

View File

@@ -72,6 +72,21 @@ bool ModelManager::isModel(const QString& file)
return false;
}
void ModelManager::cleanup()
{
if (_libraryList) {
_libraryList->clear();
}
if (_modelMap) {
for (auto& it : *_modelMap) {
// This is needed to resolve cyclic dependencies
it.second->setLibrary(nullptr);
}
_modelMap->clear();
}
}
void ModelManager::refresh()
{
_modelMap->clear();

View File

@@ -44,6 +44,7 @@ public:
ModelManager();
~ModelManager() override = default;
static void cleanup();
void refresh();
std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> getModelLibraries()