Fix majors memory leaks (TODO: shared_ptr for Game objects and implement ~Game())

This commit is contained in:
Loic Guegan 2022-02-28 18:51:47 +01:00
parent 8f1e8fa106
commit a8c59c41bc
12 changed files with 40 additions and 4 deletions

View file

@ -17,8 +17,11 @@ AppendGameDialog::AppendGameDialog(wxWindow *parent, GameBase *base)
ID_DIALOG_CANCEL_BUTTON);
Bind(wxEVT_BUTTON, &AppendGameDialog::OnImport, this,
ID_DIALOG_IMPORT_BUTTON);
Bind(wxEVT_CLOSE_WINDOW, &AppendGameDialog::OnClose, this);
}
void AppendGameDialog::OnClose(wxCloseEvent &e) { Destroy(); }
void AppendGameDialog::OnCancel(wxCommandEvent &event) { this->Close(); }
void AppendGameDialog::OnImport(wxCommandEvent &event) {

View file

@ -1,11 +1,12 @@
#include "gamebase/GameBase.hpp"
class AppendGameDialog : public DialogAppendGame {
GameBase *base;
GameBase *base; // Should not be destroy
std::vector<TabInfos *> tinfos;
public:
AppendGameDialog(wxWindow *parent, GameBase *base);
void OnCancel(wxCommandEvent &event);
void OnImport(wxCommandEvent &event);
void OnClose(wxCloseEvent &e);
};

View file

@ -24,6 +24,10 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
LoadFile();
}
BaseTab::~BaseTab() {
delete base;
}
void BaseTab::OnImport(wxCommandEvent &event) {
AppendGameDialog *dia = new AppendGameDialog(this, base);
dia->ShowModal();

View file

@ -15,6 +15,7 @@ class BaseTab : public TabBase, public TabInfos {
public:
BaseTab(wxFrame *parent, std::string base_file);
~BaseTab();
void ApplyPreferences();
void LoadFile();
void OnDelete(wxCommandEvent &event);

View file

@ -6,6 +6,10 @@ PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
pgn->FromFile(pgn_file);
}
PGNGameBase::~PGNGameBase() {
delete pgn; // Should never fail since pgn is never NULL
}
bool PGNGameBase::NextGame() {
bool game_found = false;
try {

View file

@ -8,6 +8,7 @@ class PGNGameBase : public GameBase {
public:
PGNGameBase(std::string pgn_file);
~PGNGameBase();
Game *GetGame(std::uint32_t id);
bool NextGame();
Game *GetCurrentGame();

View file

@ -30,7 +30,7 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine,
}
EngineTab::EngineTab(wxWindow *parent, std::string name)
: TabEngine(parent), TabInfos(TabInfos::ENGINE) {
: TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(NULL) {
SetLabel(name);
engineName = name;
confGroup = "engines/" + engineName;
@ -44,6 +44,14 @@ EngineTab::EngineTab(wxWindow *parent, std::string name)
Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON);
}
EngineTab::~EngineTab() {
if (engine != NULL) {
wxLogDebug("EngineTab destructor: destroying engine!");
engine->quit();
delete engine;
}
}
void EngineTab::OnDelete(wxCommandEvent &event) {
CONFIG_OPEN(conf);
conf->DeleteGroup(confGroup);

View file

@ -16,6 +16,7 @@ public:
EngineTab(wxWindow *parent, uciadapter::UCI *engine,
std::string engine_path_or_name);
EngineTab(wxWindow *parent, std::string name);
~EngineTab();
void ApplyPreferences() {}
void *GetGame() { return (NULL); }
void *GetBase() { return (NULL); }

View file

@ -14,6 +14,11 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
ApplyPreferences();
}
BoardCanvas::~BoardCanvas() {
delete t;
delete t_captures;
}
BoardCanvas::BoardCanvas(wxFrame *parent, std::uint32_t square_width,
bool frozen)
: BoardCanvas(parent) {

View file

@ -54,6 +54,7 @@ class BoardCanvas : public wxPanel {
public:
BoardCanvas(wxFrame *parent);
BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen);
~BoardCanvas();
void ApplyPreferences();
void DrawBoard(wxPaintDC &dc);
void OnPaint(wxPaintEvent &event);

View file

@ -13,6 +13,13 @@ LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name)
Bind(wxEVT_CLOSE_WINDOW, &LiveEngineDialog::OnClose, this);
}
LiveEngineDialog::~LiveEngineDialog() {
if (engine != NULL) {
wxLogDebug("LiveEngineDialog destructor: delete engine");
delete engine;
}
}
void LiveEngineDialog::InitEngine() {
if (engine == NULL) {
wxLogDebug("Start engine: %s", engine_name);
@ -51,9 +58,8 @@ void LiveEngineDialog::OnClose(wxCloseEvent &e) {
timer.Stop();
engine->stop();
engine->quit();
delete engine;
}
e.Skip();
Destroy();
}
void LiveEngineDialog::SetFEN(std::string fen) {

View file

@ -10,6 +10,7 @@ class LiveEngineDialog : public DialogLiveEngine {
public:
LiveEngineDialog(wxWindow *parent, std::string engine_name);
~LiveEngineDialog();
void InitEngine();
void TogglePauseEngine(wxCommandEvent &event);
void OnTimerTick(wxTimerEvent &event);