Ask Your Question
1

Getting a Node::SharedPtr from "this"

asked 2020-06-02 05:38:30 -0500

KenYN gravatar image

A number of functions expect a rclcpp::Node::SharedPtr, but when you are in an rclcpp::Node subclass it is not available, and of course something like foo(rclcpp::Node::SharedPtr(this)); is a great way to crash your program. Is there some registry of these SharedPtrs to query?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-06-02 11:49:45 -0500

sloretz gravatar image

rclcpp::Node inherits from std::enable_shared_from_this<>. You can get a shared pointer to a node by calling shared_from_this() in your subclass. There's more info about std::enable_shared_from_this<> here.

edit flag offensive delete link more

Comments

3

An important caveat is you can't call shared_from_this in your class's constructor because the object doesn't yet fully exist to make a shared_ptr from. There you can only use APIs that use either a raw pointer or individual node interfaces.

jdlangs gravatar image jdlangs  ( 2020-06-08 13:42:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-06-02 05:38:30 -0500

Seen: 23 times

Last updated: Jun 02