2

I create an object in a main function as a std::shared_ptr object and want to save some information of it in its destructor. However I found out, that the destructor of the object is never called. Can I manually ensure, that the destructor is called? And does anybody know, why the destructor is never called? Does this has something to do with ros2?

Here some code: main.cpp:

#include "q_learner/q_learner_node.h"

int main(int argc, char * argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::executors::MultiThreadedExecutor exec;
    auto q_learner_node = std::make_shared<q_learner::QLearnerNode>();`//This is the object that never gets destructed.
    exec.add_node(q_learner_node);
    exec.spin();
    rclcpp::shutdown();
    return 0;
}

And q_learner_node.cpp:

QLearnerNode::QLearnerNode() : rclcpp::Node("q_learner_node"){
    // ... some stuf ...
}
QLearnerNode::~QLearnerNode(){
    std::cout << "Destructor called" << std::endl;
    // ... some other stuff ...
}
// some more stuff

and q_learner_node.h:

class QLearnerNode : public rclcpp::Node
{
public:
QLearnerNode();
~QLearnerNode();

private:
// some more stuff
};

Or is it a problem of base-class / child-class?

EDIT:

I did not know this, so google gave me a hint just now:

I'm using "Ctrl+C" To exit the process in terminal. However rclcpp should provide a signal_handler to handle those signals. This does not seem to work for me. I'm still seaarching, but any hints would be great. I found this github issue from 2018, but it should not make problems in my code (I think/ hope).

LeoE
  • 2,054
  • 1
  • 11
  • 29
  • 1
    If you comment out the call to `exec.add_node(_learner_node);`, does that allow the destructor to be called when the process exits? If so, that would suggest to me that something inside the `exec` object is leaking (or otherwise keeping a copy of) the `shared_ptr` object. – Jeremy Friesner Oct 31 '19 at 14:05
  • 3
    Does `rclcpp::Node` have a virtual destructor? – AndyG Oct 31 '19 at 14:06
  • @JeremyFriesner removing `exec.add_node(q_learner_node)` does not change the behaviour. – LeoE Oct 31 '19 at 14:07
  • If you happen to have more than a single class named QLearnerNode or more than a single implementation of the destruction - it could cause the problem. – ALX23z Oct 31 '19 at 14:09
  • @AndyG No, rclcpp::Node has no virtual destructor, but reading [this](https://stackoverflow.com/questions/3899790/shared-ptr-magic) I thought the derived destructor should be called and not the base class destructor even without virtual destructor? EDIT: rclcpp::Node **does** have a virtual destructor – LeoE Oct 31 '19 at 14:12
  • 2
    @LeoE Has not? It does not seem so [according to the source code](https://github.com/ros2/rclcpp/blob/master/rclcpp/include/rclcpp/node.hpp#L100). – Daniel Langr Oct 31 '19 at 14:13
  • @ALX23z No, neither is the case. This is really all the necessary code, All I removed war couts in the main class. I don't have a second instance of the Object anywhere. – LeoE Oct 31 '19 at 14:14
  • @DanielLangr Oh, sorry, must have overlooked it, thanks! – LeoE Oct 31 '19 at 14:14
  • 2
    @AndyG As Leo suggests, that is irrelevant here. `std::make_shared` finds the correct non-virtual destructor and stores it in the smart pointer, type-erased. – Konrad Rudolph Oct 31 '19 at 14:17
  • @LeoE does it behave the same if you exit any other function than main? – ALX23z Oct 31 '19 at 14:40
  • @ALX23z sorry, I don't know what exactly you mean – LeoE Oct 31 '19 at 14:59
  • Okay, after deleting the workspace and setting everything up from scratch it all of a sudden works.. It must have been some other problem somewhere else. Thanks for all your help anyway, will vote to close this question now – LeoE Oct 31 '19 at 16:09

1 Answers1

0

I don't know what exactly caused this behavior, but after resetting the workspace and building everything from scratch it worked...

LeoE
  • 2,054
  • 1
  • 11
  • 29