From b113473070521fb8e587c6f574dc1601d75f207c Mon Sep 17 00:00:00 2001 From: Sebastian Eibl <sebastian.eibl@fau.de> Date: Wed, 13 Jun 2018 13:21:43 +0200 Subject: [PATCH] marked memory unsafe add function deprecated --- src/pe/rigidbody/BodyStorage.h | 2 +- tests/pe/BodyStorage.cpp | 19 +++++++++++-------- tests/pe/RigidBody.cpp | 4 ++-- tests/pe/SimpleCCD.cpp | 6 +++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/pe/rigidbody/BodyStorage.h b/src/pe/rigidbody/BodyStorage.h index dc4775ded..7e4475e9a 100644 --- a/src/pe/rigidbody/BodyStorage.h +++ b/src/pe/rigidbody/BodyStorage.h @@ -131,7 +131,7 @@ public: //**Add/Remove functions************************************************************************ /*!\name Add/Remove functions */ //@{ - inline RigidBody& add ( BodyID body ); + [[deprecated]] inline RigidBody& add ( BodyID body ); inline RigidBody& add ( std::unique_ptr<RigidBody>&& body ); inline iterator remove ( const id_t sid ); inline iterator remove ( BodyID body ); diff --git a/tests/pe/BodyStorage.cpp b/tests/pe/BodyStorage.cpp index 644f29a1c..498f97394 100644 --- a/tests/pe/BodyStorage.cpp +++ b/tests/pe/BodyStorage.cpp @@ -54,18 +54,21 @@ int main( int argc, char** argv ) MaterialID iron = Material::find("iron"); { BodyStorage storage; - auto bd1 = new Body1(1, iron); - auto bd2 = new Body2(2, iron); - auto bd3 = new Body2(3, iron); - auto bd4 = new Body2(4, iron); + auto bd1Ptr = std::make_unique<Body1>(1, iron); + auto bd2Ptr = std::make_unique<Body2>(2, iron); + auto bd3Ptr = std::make_unique<Body2>(3, iron); + auto bd4Ptr = std::make_unique<Body2>(4, iron); + + auto bd2 = bd2Ptr.get(); + auto bd3 = bd3Ptr.get(); WALBERLA_CHECK_EQUAL(Body1::refCount, 1); WALBERLA_CHECK_EQUAL(Body2::refCount, 3); - storage.add(bd1); - storage.add(bd2); - storage.add(bd3); - storage.add(bd4); + storage.add(std::move(bd1Ptr)); + storage.add(std::move(bd2Ptr)); + storage.add(std::move(bd3Ptr)); + storage.add(std::move(bd4Ptr)); WALBERLA_CHECK_EQUAL(storage.size(), 4); WALBERLA_CHECK_EQUAL(Body1::refCount, 1); diff --git a/tests/pe/RigidBody.cpp b/tests/pe/RigidBody.cpp index f73aa02ef..20dfb8c06 100644 --- a/tests/pe/RigidBody.cpp +++ b/tests/pe/RigidBody.cpp @@ -148,8 +148,8 @@ int main( int argc, char** argv ) MaterialID iron = Material::find("iron"); BodyStorage storage; - SphereID sphere = new Sphere(0, 0, Vec3(0,0,0), Vec3(0,0,0), Quat(), 1, iron, false, true, false); - storage.add(sphere); + SpherePtr spPtr = std::make_unique<Sphere>(0, 0, Vec3(0,0,0), Vec3(0,0,0), Quat(), 1, iron, false, true, false); + SphereID sphere = static_cast<SphereID>(&storage.add(std::move(spPtr))); Vec3 x0 = Vec3(-2,2,0); Vec3 v0 = Vec3(-1,-1,1); diff --git a/tests/pe/SimpleCCD.cpp b/tests/pe/SimpleCCD.cpp index 3e160acdd..527a84638 100644 --- a/tests/pe/SimpleCCD.cpp +++ b/tests/pe/SimpleCCD.cpp @@ -60,7 +60,7 @@ int main( int argc, char** argv ) math::seedRandomGenerator(1337); for (uint_t i = 0; i < 100; ++i) - storage[0].add( new Sphere(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 1, iron, false, false, false) ); + storage[0].add( std::make_unique<Sphere>(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 1, iron, false, false, false) ); sccd.generatePossibleContacts(); @@ -84,14 +84,14 @@ int main( int argc, char** argv ) bs.clear(); - bs.add( new Sphere(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 1, iron, false, false, false) ); + bs.add( std::make_unique<Sphere>(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 1, iron, false, false, false) ); WcTimingPool pool; for (int runs = 0; runs < 10; ++runs) { auto oldSize = bs.size(); for (uint_t i = 0; i < oldSize; ++i) - bs.add( new Sphere(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 0.5, iron, false, false, false) ); + bs.add( std::make_unique<Sphere>(UniqueID<Sphere>::createGlobal(), 0, Vec3( math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10)), math::realRandom(real_c(0), real_c(10))), Vec3(0,0,0), Quat(), 0.5, iron, false, false, false) ); pool["SCCD"].start(); sccd.generatePossibleContacts(); pool["SCCD"].end(); -- GitLab