From 4c959fe12ed2f26cbfac9646d3488cb00676fb31 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 28 Feb 2022 19:13:27 +0100 Subject: [PATCH] Improve Game and HalfMoves memory management --- src/game_tab/Game.cpp | 8 +++++++- src/game_tab/Game.hpp | 2 +- src/game_tab/GameTab.cpp | 4 ++++ src/game_tab/GameTab.hpp | 2 ++ src/game_tab/HalfMove.cpp | 5 ++++- src/ochess.hpp | 1 + 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp index e26efd7..8b52ca6 100644 --- a/src/game_tab/Game.cpp +++ b/src/game_tab/Game.cpp @@ -15,13 +15,19 @@ Game::Game(std::string fen) : current(NULL), moves(NULL), result("*") { board = chessarbiter::FENParser::Parse(fen).board; } -Game::Game(HalfMove *m, std::string initial_fen): result("*") { +Game::Game(HalfMove *m, std::string initial_fen) : result("*") { moves = m; current = m; this->initial_fen = initial_fen; board = chessarbiter::FENParser::Parse(initial_fen).board; } +Game::~Game() { + if (moves != NULL) { + delete moves; + } +} + std::string Game::GetBoard() { return (board); } std::string Game::GetTag(std::string tagname) { return (tags[tagname]); } diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index 53249fb..f0414c8 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -18,7 +18,7 @@ public: Game(); Game(std::string fen); Game(HalfMove *m, std::string initial_fen); - + ~Game(); std::string GetBoard(); std::string GetTag(std::string tagname); void SetTag(std::string tagname, std::string value); diff --git a/src/game_tab/GameTab.cpp b/src/game_tab/GameTab.cpp index c3aceb8..769681c 100644 --- a/src/game_tab/GameTab.cpp +++ b/src/game_tab/GameTab.cpp @@ -30,6 +30,10 @@ GameTab::GameTab(wxFrame *parent, Game *game) Bind(GAME_CHANGE, &GameTab::OnGameChange, this, wxID_ANY); } +GameTab::~GameTab() { + delete game; +} + void GameTab::OnGameChange(wxCommandEvent &event) { board_panel->Notify(); editor_panel->Notify(); diff --git a/src/game_tab/GameTab.hpp b/src/game_tab/GameTab.hpp index c9dcd5a..15f0514 100644 --- a/src/game_tab/GameTab.hpp +++ b/src/game_tab/GameTab.hpp @@ -17,12 +17,14 @@ class GameTab : public wxPanel, public TabInfos { GameTabRightPanel *editor_panel; GameTabLeftPanel *board_panel; Game *game; + void RefreshLabel(); void OnRefreshTabTitle(wxCommandEvent &event); void OnGameChange(wxCommandEvent &event); public: GameTab(wxFrame *parent, Game *game); + ~GameTab(); void ApplyPreferences(); void *GetGame() { return (game); } void *GetBase() { return (NULL); }; diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index f43b7f2..e366004 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -10,6 +10,9 @@ HalfMove::HalfMove(std::string move, std::string fen) : fen(fen), capture(' ') { } HalfMove::~HalfMove() { + if (mainline != NULL) { + delete mainline; + } for (HalfMove *m : variations) { delete m; } @@ -221,7 +224,7 @@ void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { BuildAndVerify(m->mainline, arbiter.GetFEN()); } for (HalfMove *v : m->variations) { - BuildAndVerify(v,fen); + BuildAndVerify(v, fen); } } void HalfMove::BuildAndVerify(std::string initial_fen) { diff --git a/src/ochess.hpp b/src/ochess.hpp index e413071..6303e31 100644 --- a/src/ochess.hpp +++ b/src/ochess.hpp @@ -11,6 +11,7 @@ #include // Check file exists etc #include #include "gui.h" +#include #define MAINWIN ((MainWindow *)wxGetApp().GetTopWindow())