Skip to content

Commit f2785f2

Browse files
committed
Fix #56: segfault when restarting the nwn2server
1 parent 211c738 commit f2785f2

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/gui/mainframe.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ MainFrame::MainFrame(wxWindow* parent, wxWindowID id, const wxString& caption, c
8585
Create( parent, id, caption, pos, size, style );
8686
}
8787

88-
MainFrame::~MainFrame() {
89-
if (controller != nullptr) {
90-
delete controller;
91-
}
92-
}
88+
MainFrame::~MainFrame() {}
9389

9490
/*!
9591
* MainFrame creator
@@ -105,7 +101,7 @@ bool MainFrame::Create(wxWindow* parent, wxWindowID id, const wxString& caption,
105101
SetIcon(GetIconResource(wxT("res/nwnx4_icon.xpm")));
106102
////@end MainFrame creation
107103

108-
m_logger = new wxLogTextCtrl(m_log);
104+
m_logger = new wxLogTextCtrl(m_log);
109105
wxLog::SetActiveTarget(m_logger);
110106

111107
wxLogMessage(wxT("Running in GUI mode."));
@@ -114,22 +110,21 @@ bool MainFrame::Create(wxWindow* parent, wxWindowID id, const wxString& caption,
114110
m_startedAt->AppendText(now.Format());
115111

116112
// open ini file
117-
std::string inifile{"nwnx.ini"};
118-
auto config = SimpleIniConfig{inifile};
113+
m_config = std::make_unique<SimpleIniConfig>("nwnx.ini");
119114

120-
controller = new NWNXController{&config};
115+
m_controller = std::make_unique<NWNXController>(m_config.get());
121116

122-
m_CmdLine->AppendText(controller->parameters);
123-
m_PWEnabled->SetValue(controller->processWatchdog);
124-
m_GWEnabled->SetValue(controller->gamespyWatchdog);
117+
m_CmdLine->AppendText(m_controller->parameters);
118+
m_PWEnabled->SetValue(m_controller->processWatchdog);
119+
m_GWEnabled->SetValue(m_controller->gamespyWatchdog);
125120
m_PWInterval->AppendText(wxT("1"));
126-
m_GWInterval->AppendText(wxString::Format(wxT("%d"), controller->gamespyInterval));
127-
m_GWRetries->AppendText(wxString::Format(wxT("%d"), controller->gamespyTolerance));
121+
m_GWInterval->AppendText(wxString::Format(wxT("%d"), m_controller->gamespyInterval));
122+
m_GWRetries->AppendText(wxString::Format(wxT("%d"), m_controller->gamespyTolerance));
128123

129124
m_BtnStop->Enable(false);
130125

131126
// Create worker thread
132-
worker = new NWNXWorker(controller, this);
127+
worker = std::make_unique<NWNXWorker>(m_controller.get(), this);
133128
if (worker->Create() != wxTHREAD_NO_ERROR)
134129
{
135130
wxLogError(wxT("Can't create worker thread!"));
@@ -444,8 +439,8 @@ void MainFrame::OnServerStopped(wxCommandEvent &event)
444439

445440
void MainFrame::OnPwEnabledClick( wxCommandEvent& event )
446441
{
447-
controller->processWatchdog = m_PWEnabled->GetValue();
448-
if (controller->processWatchdog)
442+
m_controller->processWatchdog = m_PWEnabled->GetValue();
443+
if (m_controller->processWatchdog)
449444
wxLogMessage(wxT("Process watchdog set to: enabled"));
450445
else
451446
wxLogMessage(wxT("Process watchdog set to: disabled"));
@@ -458,8 +453,8 @@ void MainFrame::OnPwEnabledClick( wxCommandEvent& event )
458453

459454
void MainFrame::OnGwEnabledClick( wxCommandEvent& event )
460455
{
461-
controller->gamespyWatchdog = m_GWEnabled->GetValue();
462-
if (controller->gamespyWatchdog)
456+
m_controller->gamespyWatchdog = m_GWEnabled->GetValue();
457+
if (m_controller->gamespyWatchdog)
463458
wxLogMessage(wxT("Gamespy watchdog set to: enabled"));
464459
else
465460
wxLogMessage(wxT("Gamespy watchdog set to: disabled"));

src/gui/mainframe.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* Includes
3232
*/
3333

34+
#include <memory>
35+
3436
#include "../controller/controller.h"
3537
#include "worker.h"
3638
#include "GuiLog.h"
@@ -170,11 +172,11 @@ class MainFrame: public wxFrame
170172
////@end MainFrame member variables
171173

172174
private:
173-
NWNXController* controller;
174-
SimpleIniConfig* configuration;
175+
std::unique_ptr<NWNXController> m_controller;
176+
std::unique_ptr<SimpleIniConfig> m_config;
175177
wxLogTextCtrl* m_logger;
176178
wxTimer m_timer;
177-
NWNXWorker* worker;
179+
std::unique_ptr<NWNXWorker> worker;
178180
};
179181

180182
#endif

src/misc/ini.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct SimpleIniConfig {
5555
}
5656

5757
template<typename T>
58-
bool Read(const std::string key, T* dest, T defaultValue) const {
58+
bool Read(const std::string key, T* dest, const T& defaultValue) const {
5959

6060
auto v = values.find(key);
6161
if (v != values.end()) {
@@ -86,7 +86,7 @@ struct SimpleIniConfig {
8686

8787
template<typename T>
8888
bool Read(const std::string key, T* dest) const {
89-
return Read(key, dest, *dest);
89+
return Read(key, dest, T{*dest});
9090
}
9191

9292
private:

0 commit comments

Comments
 (0)