terminate called after throwing an instance of 'std::bad_weak_ptr' in ROS 2: shared_from_this() in node constructors, image_transport, tf2_ros and components, measured
When a ROS 2 C++ node aborts with the std::bad_weak_ptr error below, something called shared_from_this() on a node that no std::shared_ptr owned yet, so move that call out of the constructor into an init() you call after std::make_shared, or pass this to helpers that accept it. I ran every case in this post on 2026-11-02 in the official ros:humble-ros-base, ros:jazzy-ros-base and ros:lyrical-ros-base Docker images.
[INFO] [1793582923.832702145] [ctor_body]: constructor: calling shared_from_this()
terminate called after throwing an instance of 'std::bad_weak_ptr'
what(): bad_weak_ptr
Run the node with ros2 run and you get one more line, [ros2run]: Aborted, and exit code 250. Inside a component container you get Failed to load component: Component constructor threw an exception: bad_weak_ptr instead.
In almost every report the call is in the node's own constructor, often hidden inside a helper such as image_transport::ImageTransport(shared_from_this()) or tf2_ros::TransformBroadcaster(shared_from_this()). std::make_shared only hands the object to a shared_ptr after the constructor returns, so there is nothing to share yet. On Humble, Jazzy and Lyrical, three fixes worked. You can pass this to helpers that accept it, move the code into an init() you call after make_shared, or run it from a one-shot timer. A minimal version of the second fix:
class MyNode : public rclcpp::Node {
public:
MyNode() : Node("my_node") {} // no shared_from_this() here
void init() { // call after make_shared
it_pub_ = image_transport::ImageTransport(shared_from_this()).advertise("camera/image", 1);
}
image_transport::Publisher it_pub_;
};
int main(int argc, char ** argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<MyNode>();
node->init();
rclcpp::spin(node);
rclcpp::shutdown();
}
Two common workarounds compile, then fail later. Wrapping this in a new shared_ptr crashed with a double free in 3 of 3 runs on every distro. A shared_ptr with a no-op deleter didn't crash, but it broke shared_from_this() for the rest of the node's life. Details are below.

Where shared_from_this() ran and what each case printed
Every row below threw std::bad_weak_ptr with what(): bad_weak_ptr on all three distros. When nothing caught it, the process aborted with exit code 134. The text doesn't name the class or the line, so use the table to find where the call is.
| Where shared_from_this() ran | What you see |
|---|---|
Constructor body of a class derived from rclcpp::Node |
terminate called after throwing an instance of 'std::bad_weak_ptr', exit 134 |
Member initializer list (helper_(shared_from_this())) |
same |
tf2_ros::TransformBroadcaster(shared_from_this()) in the constructor |
same |
image_transport::ImageTransport it(shared_from_this()) in the constructor |
same |
Constructor of an rclcpp_lifecycle::LifecycleNode |
same |
Node created on the stack (MyNode n;), then n.shared_from_this() |
same |
Node held by std::make_unique, then shared_from_this() |
same |
Same constructor case started with ros2 run |
same text plus [ros2run]: Aborted, exit 250 |
Constructor of a component loaded with ros2 component load |
Failed to load component: Component constructor threw an exception: bad_weak_ptr, exit 1. The container kept running. |
| The node's destructor | throws too. I caught it, so the process didn't abort. |
The stack and unique_ptr rows are the same problem from a different direction: the call came after construction, but the object was never owned by a shared_ptr. That is the cause in navigation2 issue #5335, where a bond::Bond created with std::make_unique calls shared_from_this(). Moving the unique_ptr into a shared_ptr first fixed it in my run: shared_from_this() then returned normally, with use_count=2.
Test setup: three ros-base images and four small packages
On 2026-11-02 I used the official ros:humble-ros-base, ros:jazzy-ros-base and ros:lyrical-ros-base Docker images on a 2 vCPU Linux host, one distro at a time. Inside each container I installed ros-<distro>-image-transport with apt, because ros-base doesn't include it. Then I built four small ament_cmake packages with colcon build, one worker and make -j1:
bwp: one executable with a mode argument for each cause and fix, plus a component library with a broken and a fixed component.bwp_it: image_transport in the constructor, ininit(), and through the raw-pointercreate_publisher(this, ...).bwp_it_ifaces: the node-interfaces API that Lyrical's deprecation warnings point to.
The CMake files use target_link_libraries with exported targets, not ament_target_dependencies, so the same files build on all three distros. The reason is in Unknown CMake command "ament_target_dependencies" in ROS 2 Lyrical. A helper class called NeedsNode stands in for anything whose constructor wants rclcpp::Node::SharedPtr. It keeps that pointer and creates a publisher. For each fix I recorded use_count() after setup, then dropped the owning pointer and checked through a weak_ptr whether the node was destroyed. Components were loaded into ros2 run rclcpp_components component_container.
Why the constructor can't do it
rclcpp::Node derives from std::enable_shared_from_this<Node>. shared_from_this() doesn't create ownership. It copies the ownership of a shared_ptr that already exists. std::make_shared<MyNode>() creates that shared_ptr only after MyNode() has returned, so during the constructor the hidden weak pointer is still empty. I checked this directly. In the constructor, weak_from_this().expired() returned 1 and weak_from_this().lock() returned a null pointer on all three distros. weak_from_this() doesn't throw, but it can't help you in the constructor either, because it has nothing to give you yet.
The exception is an ordinary C++ exception. Around std::make_shared<MyNode>(), catch (const std::bad_weak_ptr & e) caught it, and e.what() was just bad_weak_ptr. That is why the message is so unhelpful. Catching it doesn't fix anything, because your constructor stopped halfway through.
Fix 1: pass this, not shared_from_this()
Many helpers take the node as a template parameter or a raw pointer, not only as a shared_ptr. Where that works, it is the smallest change, because the code can stay in the constructor. In my run these all worked in the constructor on Humble, Jazzy and Lyrical:
MyNode() : Node("my_node") {
br_ = std::make_shared<tf2_ros::TransformBroadcaster>(this);
buffer_ = std::make_shared<tf2_ros::Buffer>(get_clock());
tl_ = std::make_shared<tf2_ros::TransformListener>(*buffer_, this);
img_pub_ = image_transport::create_publisher(this, "camera/image"); // deprecated on Lyrical
}
Each node built this way had use_count=1 and was destroyed as soon as I dropped my pointer. On Lyrical, image_transport 6.4.10 builds the raw-pointer call but warns: 'image_transport::Publisher image_transport::create_publisher(rclcpp::Node*, ...)' is deprecated: Use create_publisher(RequiredInterfaces node_interfaces, ..., rclcpp::QoS) instead. The ImageTransport(rclcpp::Node::SharedPtr) constructor got the same kind of warning. The replacement takes the node by reference, and the QoS argument has no default:
// Lyrical (image_transport 6.4.10): worked in the constructor
img_pub_ = image_transport::create_publisher(*this, "camera/image", rclcpp::QoS(1));
it_ = std::make_shared<image_transport::ImageTransport>(*this);
On Lyrical both lines worked in the constructor and the node was destroyed normally. The same file didn't build on Humble (image_transport 3.1.13) or Jazzy (5.1.8): cannot convert 'ItIfaces' to 'rclcpp::Node*' and no matching function for call to 'image_transport::ImageTransport::ImageTransport(ItIfaces&)'. If one source tree has to build on all three, use create_publisher(this, ...) and accept the warning on Lyrical, or use the next fix.
Fix 2: an init() you call after make_shared
When a helper only accepts Node::SharedPtr, give the node a second setup step and call it from main right after make_shared, as in the example at the top. It worked on every distro, for every helper I tried. There is one catch, and none of the error messages warn you about it. If the helper keeps the shared_ptr you gave it, and the node keeps the helper, the node owns itself and is never destroyed:
| Created in init() with shared_from_this() | Humble | Jazzy | Lyrical |
|---|---|---|---|
Helper that stores Node::SharedPtr |
use_count 2, never destroyed | use_count 2, never destroyed | use_count 2, never destroyed |
Same helper storing Node::WeakPtr |
destroyed | destroyed | destroyed |
tf2_ros::TransformBroadcaster |
destroyed | destroyed | destroyed |
tf2_ros::TransformListener |
destroyed | destroyed | destroyed |
image_transport::ImageTransport kept as a member |
use_count 2, never destroyed | use_count 2, never destroyed | destroyed |
"Never destroyed" means that after I dropped the only pointer in main, the weak_ptr still wasn't expired and the destructor didn't run. On Jazzy the leaked ImageTransport case also printed class_loader.ClassLoader: SEVERE WARNING!!! Attempting to unload library while objects created by this loader exist in the heap! at exit. For a node that lives until the process ends, this costs you only a skipped destructor. For nodes you create and destroy at run time, it is a leak. With image_transport on Humble or Jazzy, keep the image_transport::Publisher it returns, not the ImageTransport object, or use create_publisher(this, ...). For your own helpers, store Node::WeakPtr, or take the node interfaces you actually need.
Fix 3: a one-shot timer
When main isn't yours, as with components and some launch setups, nobody will call init(). You can start a 0 ms wall timer in the constructor and cancel it on its first run:
MyNode(const rclcpp::NodeOptions & options) : Node("my_node", options) {
init_timer_ = create_wall_timer(std::chrono::milliseconds(0), [this]() {
init_timer_->cancel();
helper_ = std::make_shared<Helper>(shared_from_this());
});
}
I ran this 20 times per distro with a SingleThreadedExecutor. The callback ran exactly once in 20 of 20 runs. The helper was still unset when the constructor returned, and it was set on the first spin:
| Humble | Jazzy | Lyrical | |
|---|---|---|---|
| End of constructor to timer callback, median (max) | 31 µs (84) | 53 µs (134) | 47 µs (121) |
| Start of spin to timer callback, median (max) | 21 µs (74) | 18 µs (63) | 16 µs (55) |
| Runs where it didn't fire exactly once | 0 of 20 | 0 of 20 | 0 of 20 |
The delay is tiny, but it isn't zero, and the order matters. Anything that uses the helper before the first spin finds it unset, for example code in main between make_shared and spin. I didn't test whether a message that is already waiting can be handled in the same first spin before the timer, so guard such callbacks with a null check if they use the helper.
Components and lifecycle nodes
A component whose constructor called shared_from_this() failed to load on all three distros. The container logged [ERROR] [ComponentManager]: Component constructor threw an exception: bad_weak_ptr, and ros2 component load exited with 1. The container itself kept running. A second component in the same library, written with the one-shot timer, then loaded into the same container and logged init done in one-shot timer, shared_from_this() ok.
For an rclcpp_lifecycle::LifecycleNode, the constructor failed in the same way. The cleaner place is on_configure(). A TransformBroadcaster(shared_from_this()) created there worked on every distro, and the node moved to inactive. On Lyrical the destructor also warned LifecycleNode is not shut down: Node still in state(2) in destructor, because my test exited without a shutdown transition. That warning has nothing to do with this error.
Which fix worked, and what each one costs
| Option | Result on Humble, Jazzy, Lyrical | Watch out for |
|---|---|---|
Pass this (tf2_ros, image_transport create_publisher) |
worked in the constructor, node destroyed normally | deprecated for image_transport on Lyrical; the *this plus QoS form didn't build on Humble or Jazzy |
init() after make_shared |
worked | a stored Node::SharedPtr keeps the node alive forever; you must remember to call it |
| One-shot 0 ms timer | worked, also in a component; fired once on the first spin | members are unset until the first spin |
on_configure() in a lifecycle node |
worked | only for lifecycle nodes |
Move the unique_ptr into a shared_ptr first |
worked | only for the unique_ptr case |
self_ = rclcpp::Node::SharedPtr(this) |
crashed every time (3 of 3 runs per distro) | don't use it |
shared_ptr(this, no-op deleter) given to a helper |
no crash, but shared_from_this() broke afterwards |
don't use it |
try/catch around construction |
caught std::bad_weak_ptr |
doesn't fix anything; the constructor stopped halfway |
Two workarounds that break later
A new shared_ptr around this. One question tried to build a fresh shared_ptr from the node. I stored rclcpp::Node::SharedPtr(this) in a member during the constructor. The node constructed without an error and reported use_count=1. Then, when the owner in main went out of scope, the process aborted with exit 134 in 3 of 3 runs on every distro. glibc's message differed: double free or corruption (out) on Humble, free(): invalid pointer on Jazzy and free(): invalid size on Lyrical. Two control blocks each believed they owned the same object.
A non-owning shared_ptr with a no-op deleter. Here you pass rclcpp::Node::SharedPtr(this, [](rclcpp::Node *) {}) to a helper that keeps it. Construction succeeded and nothing crashed. But the no-op pointer took over the node's hidden weak pointer, and make_shared left it in place. After construction, shared_from_this() returned a pointer from the no-op control block, not from the real owner: same_control_block=0, owner use_count=1, and shared_from_this().use_count=2. When I dropped the real owner while still holding that copy, the destructor ran anyway. The copy still reported use_count=1 for an object that no longer existed. I didn't dereference it, because that is undefined behavior. Same result on all three distros. Any later code that relies on shared_from_this() keeping the node alive, such as a timer callback that captures it, can then touch a destroyed node.
Where Humble, Jazzy and Lyrical differed
The exception, its what() text, exit 134 and the [ros2run]: Aborted line with exit 250 were the same on Humble (g++ 11.4.0), Jazzy (g++ 13.3.0) and Lyrical (g++ 15.2.0). Only these things changed:
- image_transport: Lyrical's 6.4.10 deprecates both
ImageTransport(Node::SharedPtr)andcreate_publisher(rclcpp::Node *, ...), and adds the node-interfaces overloads. Those overloads didn't exist in Humble's 3.1.13 or Jazzy's 5.1.8. - A stored
ImageTransportcreated withshared_from_this()kept the node alive on Humble and Jazzy, but not on Lyrical. - My CMakeLists set
CMAKE_CXX_STANDARD 17.__cplusplusprinted201703on Humble and Jazzy, but202002on Lyrical. The results didn't change. - The glibc message for the
shared_ptr(this)crash was different on each distro, as listed above. - On Jazzy and Lyrical, the first
ros2 component loadsometimes printedUnable to find container node '/ComponentManager', althoughros2 component listhad just shown the container. The retry worked. This is discovery timing, not this error, but it can look like a failed load.
Not tested
MoveIt (MoveGroupInterface), Nav2 and bond, other image_transport classes (CameraPublisher, subscribers), MultiThreadedExecutor and the events executor with the timer fix, launch-file composition (ComposableNodeContainer), other compilers or standard libraries (clang, libc++), RMWs other than the default Fast DDS, and Foxy, Iron, Kilted and Rolling.
Measured on 2026-11-02 with ros:humble-ros-base (sha256:d9bcf2e6f72d..., rclcpp 16.0.21, rclpy 3.3.22, ros2cli 0.18.20, rmw 6.1.4, tf2_ros 0.25.24, image_transport 3.1.13, g++ 11.4.0), ros:jazzy-ros-base (sha256:8eed0a4669bb..., rclcpp 28.1.22, rclpy 7.1.12, ros2cli 0.32.13, rmw 7.3.4, tf2_ros 0.36.23, image_transport 5.1.8, g++ 13.3.0) and ros:lyrical-ros-base (sha256:3224ee150cb0..., rclcpp 32.0.3, rclpy 10.0.11, ros2cli 0.40.9, rmw 7.10.2, tf2_ros 0.45.10, image_transport 6.4.10, g++ 15.2.0), default RMW (Fast DDS), 2 CPUs. Full digests are in digests.txt.
Script and raw output: evidence folder. Questions this answers: what(): bad_weak_ptr, Using ImageTransport in a constructor throws std::bad_weak_ptr, A constructor needs Node::SharedPtr, but you can't run shared_from_this() in the constructor, How to migrate ROS1 code that passes ros::NodeHandle to functions inside constructor, navigation2 #5335: bad_weak_ptr in createBond() due to unique_ptr with shared_from_this(), marti_common #660: bad_weak_ptr with swri_image_util.
If the abort still happens, search your package for shared_from_this() in every constructor and member initializer list, including calls passed into helpers such as ImageTransport or TransformBroadcaster. Then check that the node is created with std::make_shared, not on the stack or with std::make_unique.