Remove "this == nullptr" checks in CBOT, fixes #828

master
krzys-h 2016-10-02 21:36:59 +02:00
parent a96835e35f
commit bb9d1c8265
4 changed files with 26 additions and 20 deletions

View File

@ -92,7 +92,7 @@ void CBotClass::ClearPublic()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotClass::Purge() void CBotClass::Purge()
{ {
if ( this == nullptr ) return; assert ( this != nullptr );
delete m_pVar; delete m_pVar;
m_pVar = nullptr; m_pVar = nullptr;
@ -104,7 +104,7 @@ void CBotClass::Purge()
m_nbVar = m_parent == nullptr ? 0 : m_parent->m_nbVar; m_nbVar = m_parent == nullptr ? 0 : m_parent->m_nbVar;
m_next->Purge(); if (m_next != nullptr) m_next->Purge();
m_next = nullptr; // no longer belongs to this chain m_next = nullptr; // no longer belongs to this chain
} }
@ -205,7 +205,7 @@ std::string CBotClass::GetName()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::GetParent() CBotClass* CBotClass::GetParent()
{ {
if ( this == nullptr ) return nullptr; assert ( this != nullptr );
return m_parent; return m_parent;
} }

View File

@ -47,13 +47,13 @@ CBotProgram::CBotProgram(CBotVar* thisVar)
CBotProgram::~CBotProgram() CBotProgram::~CBotProgram()
{ {
// delete m_classes; // delete m_classes;
m_classes->Purge(); if (m_classes != nullptr) m_classes->Purge();
m_classes = nullptr; m_classes = nullptr;
CBotClass::FreeLock(this); CBotClass::FreeLock(this);
delete m_functions; delete m_functions;
m_stack->Delete(); if (m_stack != nullptr) m_stack->Delete();
} }
bool CBotProgram::Compile(const std::string& program, std::vector<std::string>& functions, void* pUser) bool CBotProgram::Compile(const std::string& program, std::vector<std::string>& functions, void* pUser)
@ -62,7 +62,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
Stop(); Stop();
// delete m_classes; // delete m_classes;
m_classes->Purge(); // purge the old definitions of classes if (m_classes != nullptr) m_classes->Purge(); // purge the old definitions of classes
// but without destroying the object // but without destroying the object
m_classes = nullptr; m_classes = nullptr;
delete m_functions; m_functions = nullptr; delete m_functions; m_functions = nullptr;
@ -226,9 +226,12 @@ bool CBotProgram::Run(void* pUser, int timer)
} }
void CBotProgram::Stop() void CBotProgram::Stop()
{
if (m_stack != nullptr)
{ {
m_stack->Delete(); m_stack->Delete();
m_stack = nullptr; m_stack = nullptr;
}
m_entryPoint = nullptr; m_entryPoint = nullptr;
CBotClass::FreeLock(this); CBotClass::FreeLock(this);
} }
@ -365,11 +368,15 @@ bool CBotProgram::RestoreState(FILE* pf)
if (!ReadString( pf, s )) return false; if (!ReadString( pf, s )) return false;
Start(s); // point de reprise Start(s); // point de reprise
if (m_stack != nullptr)
{
m_stack->Delete(); m_stack->Delete();
m_stack = nullptr; m_stack = nullptr;
}
// retrieves the stack from the memory // retrieves the stack from the memory
// uses a nullptr pointer (m_stack) but it's ok like that // uses a nullptr pointer (m_stack) but it's ok like that
// TODO: no it's not okay like that! but it looks like it doesn't get optimized out at least ~krzys_h
if (!m_stack->RestoreState(pf, m_stack)) return false; if (!m_stack->RestoreState(pf, m_stack)) return false;
m_stack->SetProgram(this); // bases for routines m_stack->SetProgram(this); // bases for routines

View File

@ -82,10 +82,10 @@ CBotStack* CBotStack::AllocateStack()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotStack::Delete() void CBotStack::Delete()
{ {
if ( this == nullptr ) return; assert ( this != nullptr );
m_next->Delete(); if (m_next != nullptr) m_next->Delete();
m_next2->Delete(); if (m_next2 != nullptr) m_next2->Delete();
if (m_prev != nullptr) if (m_prev != nullptr)
{ {
@ -192,8 +192,8 @@ bool CBotStack::Return(CBotStack* pfils)
m_var = pfils->m_var; // result transmitted m_var = pfils->m_var; // result transmitted
pfils->m_var = nullptr; // not to destroy the variable pfils->m_var = nullptr; // not to destroy the variable
m_next->Delete();m_next = nullptr; // releases the stack above if (m_next != nullptr) m_next->Delete();m_next = nullptr; // releases the stack above
m_next2->Delete();m_next2 = nullptr; // also the second stack (catch) if (m_next2 != nullptr) m_next2->Delete();m_next2 = nullptr; // also the second stack (catch)
return IsOk(); // interrupted if error return IsOk(); // interrupted if error
} }
@ -281,7 +281,7 @@ bool CBotStack::IfContinue(int state, const std::string& name)
m_state = state; // where again? m_state = state; // where again?
m_error = CBotNoErr; m_error = CBotNoErr;
m_labelBreak.clear(); m_labelBreak.clear();
m_next->Delete(); // purge above stack if (m_next != nullptr) m_next->Delete(); // purge above stack
return true; return true;
} }
@ -478,7 +478,7 @@ bool CBotStack::Execute()
if (!instr->Run(nullptr, pile)) return false; // resume interrupted execution if (!instr->Run(nullptr, pile)) return false; // resume interrupted execution
pile->m_next->Delete(); if (pile->m_next != nullptr) pile->m_next->Delete();
pile->m_callFinished = true; pile->m_callFinished = true;
return true; return true;

View File

@ -69,11 +69,10 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type)
m_instances.insert(this); m_instances.insert(this);
CBotClass* pClass = type.GetClass(); CBotClass* pClass = type.GetClass();
CBotClass* pClass2 = pClass->GetParent(); if ( pClass != nullptr && pClass->GetParent() != nullptr )
if ( pClass2 != nullptr )
{ {
// also creates an instance of the parent class // also creates an instance of the parent class
m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(),pClass2) ); //, nIdent); m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(), pClass->GetParent()) ); //, nIdent);
} }
SetClass( pClass ); SetClass( pClass );