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

@ -43,18 +43,21 @@ inline std::string fgswiftbusServiceName()
bool SwiftConnection::startServer(const SGPropertyNode* arg, SGPropertyNode* root)
{
SwiftConnection::plug = new FGSwiftBus::CPlugin();
SwiftConnection::plug = std::make_unique<FGSwiftBus::CPlugin>();
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;
SwiftConnection::plug.release();
return true;
}
@ -63,10 +66,13 @@ SwiftConnection::SwiftConnection()
init();
}
SwiftConnection::~SwiftConnection()
{
shutdown();
if (serverRunning) {
SwiftConnection::plug.release();
}
}
void SwiftConnection::init()
@ -74,10 +80,10 @@ void SwiftConnection::init()
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)
@ -90,12 +96,12 @@ void SwiftConnection::update(double delta_time_sec)
void SwiftConnection::shutdown()
{
if (initialized) {
globals->get_commands()->removeCommand("swiftStart");
globals->get_commands()->removeCommand("swiftStop");
fgSetBool("/sim/swift/available", false);
initialized = false;
}
globals->get_commands()->removeCommand("swiftStart");
globals->get_commands()->removeCommand("swiftStop");
}
}
void SwiftConnection::reinit()

View file

@ -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 <memory>
#include <thread>
#include <Main/fg_props.hxx>
#include <simgear/compiler.h>
#include <simgear/io/raw_socket.hxx>
#include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include "plugin.h"
#include "dbusconnection.h"
#include "dbusdispatcher.h"
#include "dbusserver.h"
#include <memory>
#include <thread>
#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<FGSwiftBus::CPlugin> plug{};
private:
bool serverRunning = false;
bool initialized = false;
};
#endif