From ce0b4b222d00b4aa5be5a38ff4342d54cd97b1b5 Mon Sep 17 00:00:00 2001 From: karliss Date: Sat, 7 Apr 2018 18:21:28 +0300 Subject: [PATCH] Fix double free and use of dead reference when saving (#445) --- client/lobby/CSavingScreen.cpp | 11 +++++------ client/lobby/CSavingScreen.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/client/lobby/CSavingScreen.cpp b/client/lobby/CSavingScreen.cpp index 3a97943b4..3f4bdc9d1 100644 --- a/client/lobby/CSavingScreen.cpp +++ b/client/lobby/CSavingScreen.cpp @@ -32,7 +32,7 @@ CSavingScreen::CSavingScreen() center(pos); // TODO: we should really use std::shared_ptr for passing StartInfo around. localSi = new StartInfo(*LOCPLINT->cb->getStartInfo()); - localMi = new CMapInfo(); + localMi = std::make_shared(); localMi->mapHeader = std::unique_ptr(new CMapHeader(*LOCPLINT->cb->getMapHeader())); tabSel = std::make_shared(screenType); @@ -46,12 +46,11 @@ CSavingScreen::CSavingScreen() CSavingScreen::~CSavingScreen() { - vstd::clear_pointer(localMi); } const CMapInfo * CSavingScreen::getMapInfo() { - return localMi; + return localMi.get(); } const StartInfo * CSavingScreen::getStartInfo() @@ -61,10 +60,10 @@ const StartInfo * CSavingScreen::getStartInfo() void CSavingScreen::changeSelection(std::shared_ptr to) { - if(localMi == to.get()) + if(localMi == to) return; - localMi = to.get(); + localMi = to; localSi = localMi->scenarioOptionsOfSave; card->changeSelection(); } @@ -76,7 +75,7 @@ void CSavingScreen::saveGame() std::string path = "Saves/" + tabSel->inputName->text; - auto overWrite = [&]() -> void + auto overWrite = [this, path]() -> void { Settings lastSave = settings.write["general"]["lastSave"]; lastSave->String() = path; diff --git a/client/lobby/CSavingScreen.h b/client/lobby/CSavingScreen.h index db26be082..20c04141d 100644 --- a/client/lobby/CSavingScreen.h +++ b/client/lobby/CSavingScreen.h @@ -19,7 +19,7 @@ class CSavingScreen : public CSelectionBase { public: const StartInfo * localSi; - CMapInfo * localMi; + std::shared_ptr localMi; CSavingScreen(); ~CSavingScreen();