Skip to content

Commit

Permalink
Added scope_exit for closing session
Browse files Browse the repository at this point in the history
  • Loading branch information
CihatAltiparmak committed Jul 31, 2024
1 parent 8eba2f3 commit 0566dc2
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions rmw_zenoh_cpp/src/zenohd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "rmw/error_handling.h"

#include "rcpputils/scope_exit.hpp"

static bool running = true;

class KeyboardReader final
Expand Down Expand Up @@ -192,21 +194,26 @@ int main(int argc, char ** argv)
return 1;
}

z_owned_session_t s = z_open(z_move(config));
if (!z_check(s)) {
z_owned_session_t session = z_open(z_move(config));
if (!z_check(session)) {
printf("Unable to open router session!\n");
return 1;
}
printf(
"Started Zenoh router with id %s.\n",
rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&s))).c_str());
rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&session))).c_str());
#ifdef _WIN32
SetConsoleCtrlHandler(quit, TRUE);
#else
signal(SIGINT, quit);
signal(SIGTERM, quit);
#endif

auto close_session = rcpputils::make_scope_exit(
[&session]() {
z_close(z_move(session));
});

KeyboardReader keyreader;

char c = 0;
Expand All @@ -218,8 +225,6 @@ int main(int argc, char ** argv)
c = keyreader.readOne();
} catch (const std::runtime_error &) {
perror("read():");

z_close(z_move(s));
return -1;
}

Expand All @@ -230,7 +235,5 @@ int main(int argc, char ** argv)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

z_close(z_move(s));

return 0;
}

0 comments on commit 0566dc2

Please sign in to comment.