@@ -204,7 +204,7 @@ class rmw_context_impl_s::Data final
204
204
zenoh::Session::LivelinessSubscriberOptions sub_options =
205
205
zenoh::Session::LivelinessSubscriberOptions::create_default ();
206
206
sub_options.history = true ;
207
- graph_subscriber_cpp_ = session_->liveliness_declare_subscriber (
207
+ graph_subscriber_ = session_->liveliness_declare_subscriber (
208
208
keyexpr_cpp,
209
209
[&](const zenoh::Sample & sample) {
210
210
// Look up the data shared_ptr in the global map. If it is in there, use it.
@@ -244,7 +244,7 @@ class rmw_context_impl_s::Data final
244
244
}
245
245
246
246
zenoh::ZResult result;
247
- std::move (graph_subscriber_cpp_ ).value ().undeclare (&result);
247
+ std::move (graph_subscriber_ ).value ().undeclare (&result);
248
248
if (result != Z_OK) {
249
249
RMW_ZENOH_LOG_ERROR_NAMED (
250
250
" rmw_zenoh_cpp" ,
@@ -418,7 +418,16 @@ class rmw_context_impl_s::Data final
418
418
// Graph cache.
419
419
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache_;
420
420
// ROS graph liveliness subscriber.
421
- std::optional<zenoh::Subscriber<void >> graph_subscriber_cpp_;
421
+ // The graph_subscriber *must* exist in order for anything in this Data class,
422
+ // and hence rmw_zenoh_cpp, to work.
423
+ // However, zenoh::Subscriber does not have an empty constructor,
424
+ // so just declaring this as a zenoh::Subscriber fails to compile.
425
+ // We work around that by wrapping it in a std::optional,
426
+ // so the std::optional gets constructed at Data constructor time,
427
+ // and then we initialize graph_subscriber_ later. Note that the zenoh-cpp API
428
+ // liveliness_declare_subscriber() throws an exception if it fails,
429
+ // so this should all be safe to do.
430
+ std::optional<zenoh::Subscriber<void >> graph_subscriber_;
422
431
// Equivalent to rmw_dds_common::Context's guard condition.
423
432
// Guard condition that should be triggered when the graph changes.
424
433
std::unique_ptr<rmw_guard_condition_t > graph_guard_condition_;
0 commit comments