From 87f5136714b9c490055375ae538c08be734eb43a Mon Sep 17 00:00:00 2001 From: huxingyi Date: Sat, 11 Apr 2020 17:21:52 +0930 Subject: [PATCH] Fix memory leak [skip ci] --- src/meshgenerator.cpp | 5 +++-- src/meshgenerator.h | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/meshgenerator.cpp b/src/meshgenerator.cpp index 6a499e17..79863bb2 100644 --- a/src/meshgenerator.cpp +++ b/src/meshgenerator.cpp @@ -431,8 +431,7 @@ MeshCombiner::Mesh *MeshGenerator::combinePartMesh(const QString &partIdString, partCache.previewVertices.clear(); partCache.isSuccessful = false; partCache.joined = (target == PartTarget::Model && !isDisabled); - delete partCache.mesh; - partCache.mesh = nullptr; + partCache.releaseMeshes(); struct NodeInfo { @@ -1471,6 +1470,7 @@ void MeshGenerator::generate() } m_cacheContext->partMirrorIdMap.erase(mirrorFrom); } + it->second.releaseMeshes(); it = m_cacheContext->parts.erase(it); continue; } @@ -1487,6 +1487,7 @@ void MeshGenerator::generate() } combinationIt++; } + it->second.releaseMeshes(); it = m_cacheContext->components.erase(it); continue; } diff --git a/src/meshgenerator.h b/src/meshgenerator.h index 3d53854d..6ada6d56 100644 --- a/src/meshgenerator.h +++ b/src/meshgenerator.h @@ -20,8 +20,13 @@ class GeneratedPart public: ~GeneratedPart() { - delete mesh; + releaseMeshes(); }; + void releaseMeshes() + { + delete mesh; + mesh = nullptr; + } MeshCombiner::Mesh *mesh = nullptr; std::vector vertices; std::vector> faces; @@ -63,6 +68,15 @@ public: class GeneratedCacheContext { public: + ~GeneratedCacheContext() + { + for (auto &it: cachedCombination) + delete it.second; + for (auto &it: parts) + it.second.releaseMeshes(); + for (auto &it: components) + it.second.releaseMeshes(); + } std::map components; std::map parts; std::map partMirrorIdMap;