1
0
Fork 0

Maintenance: swift_connection

Fix memory leaks.
Fix mixed use of tab/spaces for indentation.
This commit is contained in:
Scott Giese 2021-02-26 21:01:35 -06:00
parent 0ee3fd4ab6
commit 82c34590f6
2 changed files with 36 additions and 29 deletions

View file

@ -1,5 +1,5 @@
// swift_connection.cxx // swift_connection.cxx
// //
// Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/)
// Adapted to Flightgear by Lars Toenning <dev@ltoenning.de> // Adapted to Flightgear by Lars Toenning <dev@ltoenning.de>
// //
@ -43,18 +43,21 @@ inline std::string fgswiftbusServiceName()
bool SwiftConnection::startServer(const SGPropertyNode* arg, SGPropertyNode* root) bool SwiftConnection::startServer(const SGPropertyNode* arg, SGPropertyNode* root)
{ {
SwiftConnection::plug = new FGSwiftBus::CPlugin(); SwiftConnection::plug = std::make_unique<FGSwiftBus::CPlugin>();
serverRunning = true;
serverRunning = true;
fgSetBool("/sim/swift/serverRunning", true); fgSetBool("/sim/swift/serverRunning", true);
return true; return true;
} }
bool SwiftConnection::stopServer(const SGPropertyNode* arg, SGPropertyNode* root) bool SwiftConnection::stopServer(const SGPropertyNode* arg, SGPropertyNode* root)
{ {
delete SwiftConnection::plug;
SwiftConnection::plug = nullptr;
fgSetBool("/sim/swift/serverRunning", false); fgSetBool("/sim/swift/serverRunning", false);
serverRunning = false; serverRunning = false;
SwiftConnection::plug.release();
return true; return true;
} }
@ -63,39 +66,42 @@ SwiftConnection::SwiftConnection()
init(); init();
} }
SwiftConnection::~SwiftConnection() SwiftConnection::~SwiftConnection()
{ {
shutdown(); shutdown();
if (serverRunning) {
SwiftConnection::plug.release();
}
} }
void SwiftConnection::init() void SwiftConnection::init()
{ {
if (!initialized) { if (!initialized) {
globals->get_commands()->addCommand("swiftStart", this, &SwiftConnection::startServer); globals->get_commands()->addCommand("swiftStart", this, &SwiftConnection::startServer);
globals->get_commands()->addCommand("swiftStop", this, &SwiftConnection::stopServer); globals->get_commands()->addCommand("swiftStop", this, &SwiftConnection::stopServer);
fgSetBool("/sim/swift/available", true); fgSetBool("/sim/swift/available", true);
initialized = true; initialized = true;
} }
} }
void SwiftConnection::update(double delta_time_sec) void SwiftConnection::update(double delta_time_sec)
{ {
if (serverRunning) { if (serverRunning) {
SwiftConnection::plug->fastLoop(); SwiftConnection::plug->fastLoop();
} }
} }
void SwiftConnection::shutdown() void SwiftConnection::shutdown()
{ {
if (initialized) { if (initialized) {
globals->get_commands()->removeCommand("swiftStart");
globals->get_commands()->removeCommand("swiftStop");
fgSetBool("/sim/swift/available", false); fgSetBool("/sim/swift/available", false);
initialized = false; initialized = false;
}
globals->get_commands()->removeCommand("swiftStart");
globals->get_commands()->removeCommand("swiftStop");
}
} }
void SwiftConnection::reinit() void SwiftConnection::reinit()

View file

@ -1,5 +1,5 @@
// swift_connection.hxx // swift_connection.hxx
// //
// Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/)
// Adapted to Flightgear by Lars Toenning <dev@ltoenning.de> // Adapted to Flightgear by Lars Toenning <dev@ltoenning.de>
// //
@ -17,19 +17,21 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef SWIFT_CONNECTION_H #pragma once
#define SWIFT_CONNECTION_H
#include "dbusconnection.h" #include <memory>
#include "dbusdispatcher.h" #include <thread>
#include <Main/fg_props.hxx> #include <Main/fg_props.hxx>
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/io/raw_socket.hxx> #include <simgear/io/raw_socket.hxx>
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx> #include <simgear/structure/subsystem_mgr.hxx>
#include "plugin.h"
#include "dbusconnection.h"
#include "dbusdispatcher.h"
#include "dbusserver.h" #include "dbusserver.h"
#include <memory> #include "plugin.h"
#include <thread>
#ifndef NOMINMAX #ifndef NOMINMAX
#define NOMINMAX #define NOMINMAX
@ -52,11 +54,10 @@ public:
bool startServer(const SGPropertyNode* arg, SGPropertyNode* root); bool startServer(const SGPropertyNode* arg, SGPropertyNode* root);
bool stopServer(const SGPropertyNode* arg, SGPropertyNode* root); bool stopServer(const SGPropertyNode* arg, SGPropertyNode* root);
FGSwiftBus::CPlugin* plug{};
std::unique_ptr<FGSwiftBus::CPlugin> plug{};
private: private:
bool serverRunning = false; bool serverRunning = false;
bool initialized = false; bool initialized = false;
}; };
#endif