From 82c34590f6b1eb98ae8662d2c6a4b0add2d3f424 Mon Sep 17 00:00:00 2001 From: Scott Giese Date: Fri, 26 Feb 2021 21:01:35 -0600 Subject: [PATCH] Maintenance: swift_connection Fix memory leaks. Fix mixed use of tab/spaces for indentation. --- src/Network/Swift/swift_connection.cxx | 40 +++++++++++++++----------- src/Network/Swift/swift_connection.hxx | 25 ++++++++-------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/Network/Swift/swift_connection.cxx b/src/Network/Swift/swift_connection.cxx index 7ce55e4b4..34a06bfe5 100644 --- a/src/Network/Swift/swift_connection.cxx +++ b/src/Network/Swift/swift_connection.cxx @@ -1,5 +1,5 @@ -// swift_connection.cxx -// +// swift_connection.cxx +// // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) // Adapted to Flightgear by Lars Toenning // @@ -43,18 +43,21 @@ inline std::string fgswiftbusServiceName() bool SwiftConnection::startServer(const SGPropertyNode* arg, SGPropertyNode* root) { - SwiftConnection::plug = new FGSwiftBus::CPlugin(); - serverRunning = true; + SwiftConnection::plug = std::make_unique(); + + serverRunning = true; fgSetBool("/sim/swift/serverRunning", true); + return true; } bool SwiftConnection::stopServer(const SGPropertyNode* arg, SGPropertyNode* root) { - delete SwiftConnection::plug; - SwiftConnection::plug = nullptr; fgSetBool("/sim/swift/serverRunning", false); - serverRunning = false; + serverRunning = false; + + SwiftConnection::plug.release(); + return true; } @@ -63,39 +66,42 @@ SwiftConnection::SwiftConnection() init(); } - SwiftConnection::~SwiftConnection() { shutdown(); + + if (serverRunning) { + SwiftConnection::plug.release(); + } } void SwiftConnection::init() { - if (!initialized) { + if (!initialized) { globals->get_commands()->addCommand("swiftStart", this, &SwiftConnection::startServer); globals->get_commands()->addCommand("swiftStop", this, &SwiftConnection::stopServer); + fgSetBool("/sim/swift/available", true); initialized = true; - } - + } } void SwiftConnection::update(double delta_time_sec) { - if (serverRunning) { + if (serverRunning) { SwiftConnection::plug->fastLoop(); - } + } } void SwiftConnection::shutdown() { - if (initialized) { - globals->get_commands()->removeCommand("swiftStart"); - globals->get_commands()->removeCommand("swiftStop"); + if (initialized) { fgSetBool("/sim/swift/available", false); initialized = false; - } + globals->get_commands()->removeCommand("swiftStart"); + globals->get_commands()->removeCommand("swiftStop"); + } } void SwiftConnection::reinit() diff --git a/src/Network/Swift/swift_connection.hxx b/src/Network/Swift/swift_connection.hxx index c878af2d0..294fe978f 100644 --- a/src/Network/Swift/swift_connection.hxx +++ b/src/Network/Swift/swift_connection.hxx @@ -1,5 +1,5 @@ // swift_connection.hxx -// +// // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) // Adapted to Flightgear by Lars Toenning // @@ -17,19 +17,21 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -#ifndef SWIFT_CONNECTION_H -#define SWIFT_CONNECTION_H -#include "dbusconnection.h" -#include "dbusdispatcher.h" +#pragma once + +#include +#include + #include
#include #include #include #include -#include "plugin.h" + +#include "dbusconnection.h" +#include "dbusdispatcher.h" #include "dbusserver.h" -#include -#include +#include "plugin.h" #ifndef NOMINMAX #define NOMINMAX @@ -52,11 +54,10 @@ public: bool startServer(const SGPropertyNode* arg, SGPropertyNode* root); bool stopServer(const SGPropertyNode* arg, SGPropertyNode* root); - FGSwiftBus::CPlugin* plug{}; + + std::unique_ptr plug{}; private: bool serverRunning = false; - bool initialized = false; + bool initialized = false; }; - -#endif