From 3c8cad47302baf00feadc3f1c9fba6a9c3aaacef Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Thu, 3 May 2018 14:44:43 -0700 Subject: [PATCH] Fix crash from dangling surface reference. The condense operation recreates the vectors of surfaces, materials, textures and vertices so as to exclude anything that isn't referenced explicitly by a triangle. In the process, we must take care that references from other properties are cleared out. This fixes the case when a node references a mesh by id, and then the mesh is deleted because no triangle references. TODO: go through other properties and make sure the same problem doesn't exist there. It is also possible that these vectors should be replaced by maps, at least for the elements that (now) have unique IDs. --- src/RawModel.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/RawModel.cpp b/src/RawModel.cpp index 230b180..1353743 100644 --- a/src/RawModel.cpp +++ b/src/RawModel.cpp @@ -269,11 +269,20 @@ void RawModel::Condense() surfaces.clear(); + std::set survivingSurfaceIds; for (auto &triangle : triangles) { + int oldSurfaceIndex = triangle.surfaceIndex; const RawSurface &surface = oldSurfaces[triangle.surfaceIndex]; const int surfaceIndex = AddSurface(surface.name.c_str(), surface.id); surfaces[surfaceIndex] = surface; triangle.surfaceIndex = surfaceIndex; + survivingSurfaceIds.emplace(surface.id); + } + // clear out references to meshes that no longer exist + for (auto &node : nodes) { + if (node.surfaceId != 0 && survivingSurfaceIds.find(node.surfaceId) == survivingSurfaceIds.end()) { + node.surfaceId = 0; + } } }