Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mavsdk_server: fix crash on stop/destruction #2417

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/mavsdk_server/src/connection_initiator.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#pragma once

#include <atomic>
#include <chrono>
#include <future>
#include <memory>
#include <mutex>
#include <thread>
#include <string>

#include "connection_result.h"
#include "log.h"
#include "mavsdk.h"

namespace mavsdk {
namespace mavsdk_server {

template<typename Mavsdk> class ConnectionInitiator {
class ConnectionInitiator : public std::enable_shared_from_this<ConnectionInitiator> {
public:
ConnectionInitiator() {}
~ConnectionInitiator() {}

bool connect(Mavsdk& mavsdk, const std::string& connection_url)
bool connect(mavsdk::Mavsdk& mavsdk, const std::string& connection_url)
{
LogInfo() << "Waiting to discover system on " << connection_url << "...";
// Keep this class alive while this function is running.
auto self = shared_from_this();

if (!add_any_connection(mavsdk, connection_url)) {
return false;
Expand Down Expand Up @@ -49,7 +50,7 @@ template<typename Mavsdk> class ConnectionInitiator {
void cancel() { _should_exit = true; }

private:
bool add_any_connection(Mavsdk& mavsdk, const std::string& connection_url)
bool add_any_connection(mavsdk::Mavsdk& mavsdk, const std::string& connection_url)
{
mavsdk::ConnectionResult connection_result = mavsdk.add_any_connection(connection_url);

Expand Down
7 changes: 4 additions & 3 deletions src/mavsdk_server/src/mavsdk_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class MavsdkServer::Impl {

bool connect(const std::string& connection_url)
{
return _connection_initiator.connect(_mavsdk, connection_url);
_connection_initiator = std::make_shared<ConnectionInitiator>();
return _connection_initiator->connect(_mavsdk, connection_url);
}

int startGrpcServer(const int port)
Expand All @@ -31,7 +32,7 @@ class MavsdkServer::Impl {

void stop()
{
_connection_initiator.cancel();
_connection_initiator->cancel();

if (_server != nullptr) {
_server->stop();
Expand All @@ -47,7 +48,7 @@ class MavsdkServer::Impl {

private:
mavsdk::Mavsdk _mavsdk;
ConnectionInitiator<mavsdk::Mavsdk> _connection_initiator;
std::shared_ptr<ConnectionInitiator> _connection_initiator;
std::unique_ptr<GrpcServer> _server;
int _grpc_port;
};
Expand Down
Loading