From 48666c4604a5fbe897c7609767e4ee1ca4da5b35 Mon Sep 17 00:00:00 2001 From: melex750 Date: Wed, 3 Aug 2016 17:42:10 -0400 Subject: [PATCH] Fix not calling destructors after save/reload --- src/CBot/CBotStack.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 3170fb72..27c648f2 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -760,7 +760,17 @@ bool CBotVar::Save0State(FILE* pf) if (!WriteWord(pf, 100+static_cast(m_mPrivate)))return false; // private variable? if (!WriteWord(pf, m_bStatic))return false; // static variable? if (!WriteWord(pf, m_type.GetType()))return false; // saves the type (always non-zero) - if (!WriteWord(pf, static_cast(m_binit))) return false; // variable defined? + + if (m_type.Eq(CBotTypPointer) && GetPointer() != nullptr) + { + if (GetPointer()->m_bConstructor) // constructor was called? + { + if (!WriteWord(pf, (2000 + static_cast(m_binit)) )) return false; + return WriteString(pf, m_token->GetString()); // and variable name + } + } + + if (!WriteWord(pf, static_cast(m_binit))) return false; // variable defined? return WriteString(pf, m_token->GetString()); // and variable name } @@ -800,6 +810,13 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) if ( w == CBotTypClass ) w = CBotTypIntrinsic; // necessarily intrinsic if (!ReadWord(pf, wi)) return false; // init ? + bool bConstructor = false; + if (w == CBotTypPointer && wi >= 2000) + { + wi -= 2000; + bConstructor = true; + } + CBotVar::InitType initType = static_cast(wi); if (!ReadString(pf, name)) return false; // variable name @@ -868,9 +885,10 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) case CBotTypPointer: case CBotTypNullPointer: - if (!ReadString(pf, s)) return false; + if (!ReadString(pf, s)) return false; // name of the class { - pNew = CBotVar::Create(token, CBotTypResult(w, s));// creates a variable + CBotTypResult ptrType(w, s); + pNew = CBotVar::Create(token, ptrType);// creates a variable // CBotVarClass* p = nullptr; long id; ReadLong(pf, id); @@ -881,6 +899,8 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; (static_cast(pNew))->SetPointer( pInstance ); // and point over + if (bConstructor) pNew->ConstructorSet(); // constructor was called + // if ( p != nullptr ) (static_cast(pNew))->SetPointer( p ); // rather this one }