From c91ef34a70ecc4176b2bfa809c9feb03a8981260 Mon Sep 17 00:00:00 2001 From: elsid Date: Wed, 4 Aug 2021 11:35:53 +0200 Subject: [PATCH 1/2] Avoid using a specific type for stored ref_ptr to extend lifetime --- components/detournavigator/recastmeshobject.cpp | 12 ++++++------ components/detournavigator/recastmeshobject.hpp | 13 ++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/components/detournavigator/recastmeshobject.cpp b/components/detournavigator/recastmeshobject.cpp index 862460b34b..8b4bc2fd6f 100644 --- a/components/detournavigator/recastmeshobject.cpp +++ b/components/detournavigator/recastmeshobject.cpp @@ -23,35 +23,35 @@ namespace DetourNavigator return result; } - std::vector makeChildrenObjects(const osg::ref_ptr& instance, + std::vector makeChildrenObjects(const osg::ref_ptr& holder, const btCompoundShape& shape, const AreaType areaType) { std::vector result; for (int i = 0, num = shape.getNumChildShapes(); i < num; ++i) { - const CollisionShape collisionShape {instance, *shape.getChildShape(i)}; + const CollisionShape collisionShape {holder, *shape.getChildShape(i)}; result.emplace_back(collisionShape, shape.getChildTransform(i), areaType); } return result; } - std::vector makeChildrenObjects(const osg::ref_ptr& instance, + std::vector makeChildrenObjects(const osg::ref_ptr& holder, const btCollisionShape& shape, const AreaType areaType) { if (shape.isCompound()) - return makeChildrenObjects(std::move(instance), static_cast(shape), areaType); + return makeChildrenObjects(holder, static_cast(shape), areaType); return std::vector(); } } RecastMeshObject::RecastMeshObject(const CollisionShape& shape, const btTransform& transform, const AreaType areaType) - : mShapeInstance(shape.getShapeInstance()) + : mHolder(shape.getHolder()) , mShape(shape.getShape()) , mTransform(transform) , mAreaType(areaType) , mLocalScaling(mShape.get().getLocalScaling()) - , mChildren(makeChildrenObjects(mShapeInstance, mShape.get(), mAreaType)) + , mChildren(makeChildrenObjects(mHolder, mShape.get(), mAreaType)) { } diff --git a/components/detournavigator/recastmeshobject.hpp b/components/detournavigator/recastmeshobject.hpp index 81199c5bad..445dfe50f5 100644 --- a/components/detournavigator/recastmeshobject.hpp +++ b/components/detournavigator/recastmeshobject.hpp @@ -3,11 +3,10 @@ #include "areatype.hpp" -#include - #include #include +#include #include #include @@ -20,16 +19,16 @@ namespace DetourNavigator class CollisionShape { public: - CollisionShape(osg::ref_ptr instance, const btCollisionShape& shape) - : mShapeInstance(std::move(instance)) + CollisionShape(osg::ref_ptr holder, const btCollisionShape& shape) + : mHolder(std::move(holder)) , mShape(shape) {} - const osg::ref_ptr& getShapeInstance() const { return mShapeInstance; } + const osg::ref_ptr& getHolder() const { return mHolder; } const btCollisionShape& getShape() const { return mShape; } private: - osg::ref_ptr mShapeInstance; + osg::ref_ptr mHolder; std::reference_wrapper mShape; }; @@ -56,7 +55,7 @@ namespace DetourNavigator } private: - osg::ref_ptr mShapeInstance; + osg::ref_ptr mHolder; std::reference_wrapper mShape; btTransform mTransform; AreaType mAreaType; From 41b02ff1aa9a4f07d0ef61d79a9c9521d4709df8 Mon Sep 17 00:00:00 2001 From: elsid Date: Wed, 4 Aug 2021 18:35:25 +0200 Subject: [PATCH 2/2] Copy only required RecastMeshObject fields --- .../detournavigator/recastmeshmanager.cpp | 17 +++++++++++++---- components/detournavigator/recastmeshobject.hpp | 5 +++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/components/detournavigator/recastmeshmanager.cpp b/components/detournavigator/recastmeshmanager.cpp index 5bbbbe4dea..e4e5b76449 100644 --- a/components/detournavigator/recastmeshmanager.cpp +++ b/components/detournavigator/recastmeshmanager.cpp @@ -123,7 +123,13 @@ namespace DetourNavigator tileBounds.mMin /= mSettings.mRecastScaleFactor; tileBounds.mMax /= mSettings.mRecastScaleFactor; RecastMeshBuilder builder(tileBounds); - std::vector objects; + using Object = std::tuple< + osg::ref_ptr, + std::reference_wrapper, + btTransform, + AreaType + >; + std::vector objects; std::size_t revision; { const std::lock_guard lock(mMutex); @@ -133,11 +139,14 @@ namespace DetourNavigator std::visit(AddHeightfield {v.mCell, builder}, v.mShape); objects.reserve(mObjects.size()); for (const auto& [k, object] : mObjects) - objects.push_back(object.getImpl()); + { + const RecastMeshObject& impl = object.getImpl(); + objects.emplace_back(impl.getHolder(), impl.getShape(), impl.getTransform(), impl.getAreaType()); + } revision = mRevision; } - for (const auto& v : objects) - builder.addObject(v.getShape(), v.getTransform(), v.getAreaType()); + for (const auto& [holder, shape, transform, areaType] : objects) + builder.addObject(shape, transform, areaType); return std::move(builder).create(mGeneration, revision); } diff --git a/components/detournavigator/recastmeshobject.hpp b/components/detournavigator/recastmeshobject.hpp index 445dfe50f5..0c50c2f346 100644 --- a/components/detournavigator/recastmeshobject.hpp +++ b/components/detournavigator/recastmeshobject.hpp @@ -39,6 +39,11 @@ namespace DetourNavigator bool update(const btTransform& transform, const AreaType areaType); + const osg::ref_ptr& getHolder() const + { + return mHolder; + } + const btCollisionShape& getShape() const { return mShape;