Skip to content

Commit

Permalink
Fixed crashes when exiting application due to node still being used.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanFabian committed Oct 23, 2024
1 parent 0429f32 commit e863e5c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion include/qml_ros2_plugin/ros2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace qml_ros2_plugin
{

class Ros2Qml : public QObject
class Ros2Qml final : public QObject
{
Q_OBJECT
private:
Expand All @@ -31,6 +31,8 @@ class Ros2Qml : public QObject

Ros2Qml( const Ros2Qml & ) = delete;

~Ros2Qml() final;

void operator=( const Ros2Qml & ) = delete;

/*!
Expand Down
12 changes: 12 additions & 0 deletions src/ros2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ Ros2Qml &Ros2Qml::getInstance()

Ros2Qml::Ros2Qml() : count_wrappers( 0 ) { babel_fish_ = BabelFishDispenser::getBabelFish(); }

Ros2Qml::~Ros2Qml()
{
if ( node_ == nullptr )
return;
QML_ROS2_PLUGIN_DEBUG( "Ros2Qml destructing but context still alive. Shutting down context." );
node_ = nullptr;
rclcpp::shutdown( context_, "QML Ros2 was destroyed." );
if ( executor_thread_.joinable() )
executor_thread_.join();
context_ = nullptr;
}

bool Ros2Qml::isInitialized() const { return context_ != nullptr; }

void Ros2Qml::init( const QString &name, quint32 options )
Expand Down
2 changes: 1 addition & 1 deletion src/subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void Subscription::subscribe()
void Subscription::try_subscribe()
{
std::shared_ptr<rclcpp::Node> node = Ros2Qml::getInstance().node();
if ( node == nullptr )
if ( node == nullptr || !Ros2Qml::getInstance().ok() )
return;
if ( user_message_type_.isEmpty() ) {
subscription_ = babel_fish_.create_subscription(
Expand Down

0 comments on commit e863e5c

Please sign in to comment.