Files
create/src/Mod/Material/App/ModelManager.h
wmayer a644f75d53 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
2024-04-25 15:47:09 +02:00

82 lines
3.1 KiB
C++

/***************************************************************************
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef MATERIAL_MODELMANAGER_H
#define MATERIAL_MODELMANAGER_H
#include <memory>
#include <Mod/Material/MaterialGlobal.h>
#include <QMutex>
#include "Exceptions.h"
#include "FolderTree.h"
#include "Model.h"
#include "ModelLibrary.h"
namespace Materials
{
class MaterialsExport ModelManager: public Base::BaseClass
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
ModelManager();
~ModelManager() override = default;
static void cleanup();
void refresh();
std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> getModelLibraries()
{
return _libraryList;
}
std::shared_ptr<std::map<QString, std::shared_ptr<Model>>> getModels()
{
return _modelMap;
}
std::shared_ptr<std::map<QString, std::shared_ptr<ModelTreeNode>>>
getModelTree(std::shared_ptr<ModelLibrary> library, ModelFilter filter = ModelFilter_None) const
{
return library->getModelTree(filter);
}
std::shared_ptr<Model> getModel(const QString& uuid) const;
std::shared_ptr<Model> getModelByPath(const QString& path) const;
std::shared_ptr<Model> getModelByPath(const QString& path, const QString& lib) const;
std::shared_ptr<ModelLibrary> getLibrary(const QString& name) const;
static bool isModel(const QString& file);
static bool passFilter(ModelFilter filter, Model::ModelType modelType);
private:
static void initLibraries();
static std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> _libraryList;
static std::shared_ptr<std::map<QString, std::shared_ptr<Model>>> _modelMap;
static QMutex _mutex;
};
} // namespace Materials
#endif // MATERIAL_MODELMANAGER_H