From 48195def811e7c9a4ec6766b44b3d539bfc977e2 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 23 Feb 2023 02:57:47 -0800 Subject: [PATCH] clean up rendering resources (#324) Signed-off-by: Ian Chen --- src/RenderingSensor.cc | 2 +- test/integration/depth_camera_plugin.cc | 12 +++++------ test/integration/gpu_lidar_sensor_plugin.cc | 24 ++++++++++++++++++--- test/integration/rgbd_camera_plugin.cc | 10 +++++---- test/integration/thermal_camera_plugin.cc | 10 +++++---- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/RenderingSensor.cc b/src/RenderingSensor.cc index 4666ca0e..a9ce059b 100644 --- a/src/RenderingSensor.cc +++ b/src/RenderingSensor.cc @@ -47,7 +47,7 @@ RenderingSensor::RenderingSensor() : ////////////////////////////////////////////////// RenderingSensor::~RenderingSensor() { - if (!this->dataPtr->scene) + if (!this->dataPtr->scene || !this->dataPtr->scene->IsInitialized()) return; for (auto &s : this->dataPtr->sensors) { diff --git a/test/integration/depth_camera_plugin.cc b/test/integration/depth_camera_plugin.cc index f38fc9dc..b8f50c02 100644 --- a/test/integration/depth_camera_plugin.cc +++ b/test/integration/depth_camera_plugin.cc @@ -222,6 +222,7 @@ void DepthCameraSensorTest::ImagesWithBuiltinSDF( box->SetLocalRotation(0, 0, 0); box->SetLocalScale(unitBoxSize, unitBoxSize, unitBoxSize); box->SetMaterial(blue); + scene->DestroyMaterial(blue); root->AddChild(box); // do the test @@ -333,11 +334,9 @@ void DepthCameraSensorTest::ImagesWithBuiltinSDF( EXPECT_EQ(9, infoMsg.rectification_matrix().size()); // Check that for a box really close it returns -inf - root->RemoveChild(box); gz::math::Vector3d boxPositionNear( unitBoxSize * 0.5 + near_ * 0.5, 0.0, 0.0); box->SetLocalPosition(boxPositionNear); - root->AddChild(box); mgr.RunOnce(gz::common::Time::Zero, true); for (int sleep = 0; @@ -369,11 +368,9 @@ void DepthCameraSensorTest::ImagesWithBuiltinSDF( g_mutex.unlock(); // Check that for a box really far it returns inf - root->RemoveChild(box); gz::math::Vector3d boxPositionFar( unitBoxSize * 0.5 + far_ * 1.5, 0.0, 0.0); box->SetLocalPosition(boxPositionFar); - root->AddChild(box); mgr.RunOnce(gz::common::Time::Zero, true); for (int sleep = 0; @@ -405,11 +402,9 @@ void DepthCameraSensorTest::ImagesWithBuiltinSDF( // Check that the depth values for a box do not warp. - root->RemoveChild(box); gz::math::Vector3d boxPositionFillFrame( unitBoxSize * 0.5 + 0.2, 0.0, 0.0); box->SetLocalPosition(boxPositionFillFrame); - root->AddChild(box); mgr.RunOnce(gz::common::Time::Zero, true); for (int sleep = 0; @@ -494,7 +489,12 @@ void DepthCameraSensorTest::ImagesWithBuiltinSDF( g_mutex.unlock(); g_pcMutex.unlock(); + // clean up rendering ptrs + blue.reset(); + box.reset(); + // Clean up + mgr.Remove(depthSensor->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } diff --git a/test/integration/gpu_lidar_sensor_plugin.cc b/test/integration/gpu_lidar_sensor_plugin.cc index db3a02a9..28e2144e 100644 --- a/test/integration/gpu_lidar_sensor_plugin.cc +++ b/test/integration/gpu_lidar_sensor_plugin.cc @@ -277,6 +277,7 @@ void GpuLidarSensorTest::CreateGpuLidar(const std::string &_renderEngine) // Clean up c.reset(); + mgr.Remove(sensor->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } @@ -414,8 +415,11 @@ void GpuLidarSensorTest::DetectBox(const std::string &_renderEngine) EXPECT_TRUE(pointMsgs.back().is_dense()); EXPECT_EQ(32u * horzSamples * vertSamples, pointMsgs.back().data().size()); + // Clean up rendering ptrs + visualBox1.reset(); + // Clean up - // + mgr.Remove(sensor->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } @@ -567,7 +571,14 @@ void GpuLidarSensorTest::TestThreeBoxes(const std::string &_renderEngine) for (unsigned int i = 0; i < sensor1->RayCount(); ++i) EXPECT_DOUBLE_EQ(sensor2->Range(i), gz::math::INF_D); + // Clean up rendering ptrs + visualBox1.reset(); + visualBox2.reset(); + visualBox3.reset(); + // Clean up + mgr.Remove(sensor1->Id()); + mgr.Remove(sensor2->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } @@ -687,7 +698,11 @@ void GpuLidarSensorTest::VerticalLidar(const std::string &_renderEngine) } } + // Clean up rendering ptrs + visualBox1.reset(); + // Clean up + mgr.Remove(sensor->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } @@ -809,8 +824,12 @@ void GpuLidarSensorTest::ManualUpdate(const std::string &_renderEngine) EXPECT_DOUBLE_EQ(sensor2->Range(last), gz::math::INF_D); #endif + // Clean up rendering ptrs + visualBox1.reset(); + // Clean up - // + mgr.Remove(sensor1->Id()); + mgr.Remove(sensor2->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); } @@ -849,7 +868,6 @@ void GpuLidarSensorTest::Topic(const std::string &_renderEngine) // Create a GpuLidarSensor gz::sensors::Manager mgr; - // Default topic { const std::string topic; diff --git a/test/integration/rgbd_camera_plugin.cc b/test/integration/rgbd_camera_plugin.cc index e0ced44b..4a329976 100644 --- a/test/integration/rgbd_camera_plugin.cc +++ b/test/integration/rgbd_camera_plugin.cc @@ -242,6 +242,7 @@ void RgbdCameraSensorTest::ImagesWithBuiltinSDF( box->SetLocalRotation(0, 0, 0); box->SetLocalScale(unitBoxSize, unitBoxSize, unitBoxSize); box->SetMaterial(blue); + scene->DestroyMaterial(blue); root->AddChild(box); // do the test @@ -526,11 +527,9 @@ void RgbdCameraSensorTest::ImagesWithBuiltinSDF( g_imgMutex.unlock(); // Check that for a box really close it returns -inf - root->RemoveChild(box); math::Vector3d boxPositionNear( unitBoxSize * 0.5 + near_ * 0.5, 0.0, 0.0); box->SetLocalPosition(boxPositionNear); - root->AddChild(box); mgr.RunOnce(common::Time::Zero, true); for (int sleep = 0; sleep < 300 && @@ -632,11 +631,9 @@ void RgbdCameraSensorTest::ImagesWithBuiltinSDF( g_pcMutex.unlock(); // Check that for a box really far it returns inf - root->RemoveChild(box); math::Vector3d boxPositionFar( unitBoxSize * 0.5 + far_ * 1.5, 0.0, 0.0); box->SetLocalPosition(boxPositionFar); - root->AddChild(box); mgr.RunOnce(common::Time::Zero, true); for (int sleep = 0; sleep < 300 && @@ -737,7 +734,12 @@ void RgbdCameraSensorTest::ImagesWithBuiltinSDF( g_imgMutex.unlock(); g_pcMutex.unlock(); + // Clean up rendering ptrs + box.reset(); + blue.reset(); + // Clean up + mgr.Remove(rgbdSensor->Id()); engine->DestroyScene(scene); rendering::unloadEngine(engine->Name()); } diff --git a/test/integration/thermal_camera_plugin.cc b/test/integration/thermal_camera_plugin.cc index 5b1f71c4..bb3e9118 100644 --- a/test/integration/thermal_camera_plugin.cc +++ b/test/integration/thermal_camera_plugin.cc @@ -262,11 +262,9 @@ void ThermalCameraSensorTest::ImagesWithBuiltinSDF( EXPECT_EQ(9, infoMsg.rectification_matrix().size()); // Check that for a box really close it returns box temperature - root->RemoveChild(box); gz::math::Vector3d boxPositionNear( unitBoxSize * 0.5 + near_ * 0.5, 0.0, 0.0); box->SetLocalPosition(boxPositionNear); - root->AddChild(box); mgr.RunOnce(gz::common::Time::Zero, true); for (int sleep = 0; @@ -296,11 +294,9 @@ void ThermalCameraSensorTest::ImagesWithBuiltinSDF( g_mutex.unlock(); // Check that for a box really far it returns ambient temperature - root->RemoveChild(box); gz::math::Vector3d boxPositionFar( unitBoxSize * 0.5 + far_ * 1.5, 0.0, 0.0); box->SetLocalPosition(boxPositionFar); - root->AddChild(box); mgr.RunOnce(gz::common::Time::Zero, true); for (int sleep = 0; @@ -329,8 +325,14 @@ void ThermalCameraSensorTest::ImagesWithBuiltinSDF( g_infoMutex.unlock(); g_mutex.unlock(); + delete [] g_thermalBuffer; + g_thermalBuffer = nullptr; + + // Clean up rendering ptrs + box.reset(); // Clean up + mgr.Remove(thermalSensor->Id()); engine->DestroyScene(scene); gz::rendering::unloadEngine(engine->Name()); }