[colobot] 43/100: Fix memory leaks in CBOT engine

Didier Raboud odyx at moszumanska.debian.org
Thu Jun 1 18:10:17 UTC 2017


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch debian/master
in repository colobot.

commit 8e54f7ca9c32ed3fa599a9171ad046ecace9f67f
Author: melex750 <melex750 at users.noreply.github.com>
Date:   Mon Jan 16 13:00:01 2017 -0500

    Fix memory leaks in CBOT engine
---
 src/CBot/CBotClass.cpp                  | 16 +++++++++++-----
 src/CBot/CBotInstr/CBotDefClass.cpp     |  5 ++++-
 src/CBot/CBotInstr/CBotFunction.cpp     |  1 +
 src/CBot/CBotInstr/CBotInstrCall.cpp    |  2 ++
 src/CBot/CBotInstr/CBotInstrMethode.cpp |  2 ++
 src/CBot/CBotInstr/CBotNew.cpp          |  3 +++
 src/CBot/CBotStack.cpp                  |  4 ++--
 src/CBot/CBotTypResult.cpp              |  7 ++++++-
 src/CBot/CBotVar/CBotVar.cpp            |  3 +++
 9 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp
index 127d6d3..c19cbf4 100644
--- a/src/CBot/CBotClass.cpp
+++ b/src/CBot/CBotClass.cpp
@@ -83,7 +83,11 @@ CBotClass* CBotClass::Create(const std::string& name,
 ////////////////////////////////////////////////////////////////////////////////
 void CBotClass::ClearPublic()
 {
-    m_publicClasses.clear();
+    while ( !m_publicClasses.empty() )
+    {
+        auto it = m_publicClasses.begin();
+        delete *it; // calling destructor removes the class from the list
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -593,17 +597,16 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                 else
                 {
                     // return a method precompiled in pass 1
-                    CBotToken* ppp = p;
                     CBotCStack* pStk = pStack->TokenStack(nullptr, true);
                     CBotDefParam* params = CBotDefParam::Compile(p, pStk );
                     delete pStk;
-                    p = ppp;
                     std::list<CBotFunction*>::iterator pfIter = std::find_if(m_pMethod.begin(), m_pMethod.end(), [&pp, &params](CBotFunction* x)
                     {
                         return x->GetName() == pp && x->CheckParam( params );
                     });
                     assert(pfIter != m_pMethod.end());
                     CBotFunction* pf = *pfIter;
+                    delete params;
 
                     bool bConstructor = (pp == GetName());
                     CBotCStack* pile = pStack->TokenStack(nullptr, true);
@@ -690,7 +693,6 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                     if (i == nullptr || pStack->GetType() != CBotTypInt) // must be a number
                     {
                         pStack->SetError(CBotErrBadIndex, p->GetStart());
-                        return false;
                     }
                 }
                 else
@@ -701,8 +703,9 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                 if (limites == nullptr) limites = i;
                 else limites->AddNext3(i);
 
-                if (IsOfType(p, ID_CLBRK)) continue;
+                if (pStack->IsOk() && IsOfType(p, ID_CLBRK)) continue;
                 pStack->SetError(CBotErrCloseIndex, p->GetStart());
+                delete limites;
                 return false;
             }
 
@@ -775,7 +778,10 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                 }
             }
             else
+            {
                 delete i;
+                delete limites;
+            }
 
             if ( IsOfType(p, ID_COMMA) ) continue;
             if ( IsOfType(p, ID_SEP) ) break;
diff --git a/src/CBot/CBotInstr/CBotDefClass.cpp b/src/CBot/CBotInstr/CBotDefClass.cpp
index 3fe832e..bd885e1 100644
--- a/src/CBot/CBotInstr/CBotDefClass.cpp
+++ b/src/CBot/CBotInstr/CBotDefClass.cpp
@@ -51,6 +51,9 @@ CBotDefClass::CBotDefClass()
 ////////////////////////////////////////////////////////////////////////////////
 CBotDefClass::~CBotDefClass()
 {
+    delete m_parameters;
+    delete m_exprRetVar;
+    delete m_expr;
     delete m_var;
 }
 
@@ -246,9 +249,9 @@ bool CBotDefClass::Execute(CBotStack* &pj)
 
     if (m_exprRetVar != nullptr) // Class c().method();
     {
-        if (pile->IfStep()) return false;
         if (pile->GetState() == 4)
         {
+            if (pile->IfStep()) return false;
             CBotStack* pile3 = pile->AddStack();
             if (!m_exprRetVar->Execute(pile3)) return false;
             pile3->SetVar(nullptr);
diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp
index 7852c7e..ddbd366 100644
--- a/src/CBot/CBotInstr/CBotFunction.cpp
+++ b/src/CBot/CBotInstr/CBotFunction.cpp
@@ -185,6 +185,7 @@ CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunct
 
             }
             func->m_openpar = *p;
+            delete func->m_param;
             func->m_param = CBotDefParam::Compile(p, pStk );
             func->m_closepar = *(p->GetPrev());
             if (pStk->IsOk())
diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp
index 6b45bb5..86e37f7 100644
--- a/src/CBot/CBotInstr/CBotInstrCall.cpp
+++ b/src/CBot/CBotInstr/CBotInstrCall.cpp
@@ -36,6 +36,7 @@ namespace CBot
 CBotInstrCall::CBotInstrCall()
 {
     m_parameters = nullptr;
+    m_exprRetVar = nullptr;
     m_nFuncIdent = 0;
 }
 
@@ -43,6 +44,7 @@ CBotInstrCall::CBotInstrCall()
 CBotInstrCall::~CBotInstrCall()
 {
     delete m_parameters;
+    delete m_exprRetVar;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp
index 3a41802..f4cda03 100644
--- a/src/CBot/CBotInstr/CBotInstrMethode.cpp
+++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp
@@ -36,6 +36,7 @@ namespace CBot
 CBotInstrMethode::CBotInstrMethode()
 {
     m_parameters = nullptr;
+    m_exprRetVar = nullptr;
     m_MethodeIdent = 0;
 }
 
@@ -43,6 +44,7 @@ CBotInstrMethode::CBotInstrMethode()
 CBotInstrMethode::~CBotInstrMethode()
 {
     delete m_parameters;
+    delete m_exprRetVar;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp
index ddd50c6..9fab382 100644
--- a/src/CBot/CBotInstr/CBotNew.cpp
+++ b/src/CBot/CBotInstr/CBotNew.cpp
@@ -36,12 +36,15 @@ namespace CBot
 CBotNew::CBotNew()
 {
     m_parameters = nullptr;
+    m_exprRetVar = nullptr;
     m_nMethodeIdent = 0;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 CBotNew::~CBotNew()
 {
+    delete m_parameters;
+    delete m_exprRetVar;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp
index 6afcc75..70ec563 100644
--- a/src/CBot/CBotStack.cpp
+++ b/src/CBot/CBotStack.cpp
@@ -737,11 +737,11 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack)
 {
     unsigned short w;
 
-    pStack = nullptr;
+    if (pStack != this) pStack = nullptr;
     if (!ReadWord(pf, w)) return false;
     if ( w == 0 ) return true; // 0 - terminator
 
-    pStack = AddStack();
+    if (pStack == nullptr) pStack = AddStack();
 
     if ( w == 2 ) // 2 - m_next2
     {
diff --git a/src/CBot/CBotTypResult.cpp b/src/CBot/CBotTypResult.cpp
index 46d42ee..d7005c2 100644
--- a/src/CBot/CBotTypResult.cpp
+++ b/src/CBot/CBotTypResult.cpp
@@ -167,11 +167,16 @@ CBotTypResult& CBotTypResult::operator=(const CBotTypResult& src)
     m_type = src.m_type;
     m_limite = src.m_limite;
     m_class = src.m_class;
-    m_next = nullptr;
     if (src.m_next != nullptr )
     {
+        delete m_next;
         m_next = new CBotTypResult(*src.m_next);
     }
+    else
+    {
+        delete m_next;
+        m_next = nullptr;
+    }
     return *this;
 }
 
diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp
index 4e88cd6..7ea659f 100644
--- a/src/CBot/CBotVar/CBotVar.cpp
+++ b/src/CBot/CBotVar/CBotVar.cpp
@@ -22,6 +22,7 @@
 
 #include "CBot/CBotStack.h"
 
+#include "CBot/CBotInstr/CBotInstr.h"
 #include "CBot/CBotVar/CBotVarArray.h"
 #include "CBot/CBotVar/CBotVarPointer.h"
 #include "CBot/CBotVar/CBotVarClass.h"
@@ -70,6 +71,8 @@ CBotVar::CBotVar(const CBotToken &name) : CBotVar()
 CBotVar::~CBotVar( )
 {
     delete  m_token;
+    delete  m_InitExpr;
+    delete  m_LimExpr;
 }
 
 ////////////////////////////////////////////////////////////////////////////////

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list