Menu ▾ ▴

#2 Memory leak in Clear() function

v1.0_(example)
open
nobody
Memory leak (1)
5
2020-12-31
2020-12-31
No

Hi,
CFiniteStateMachine::Clear() function doesn't clear all states, transions and variables.
This function is called by the destructor to clean up, but since it clear the 1st state and varibale.
Thus, a memory leak occurs.
The correction is simple, just replace the 'if's by while.
Here below the correct Cler Function:
bool CFiniteStateMachine::Clear()
{
bool res = false;
if (!m_Running)
{
// delete states
State_Map::iterator s = m_States.begin();
while(!(s==m_States.end()))
{
delete (s).second;
(
s).second = NULL;
m_States.erase(s);
s = m_States.begin(); // The first is erased. Now get the new begin.

    }

    // delete varaibles
    Var_Map::iterator v = m_Variables.begin();
    while(!(v==m_Variables.end()))
    {
        (*v).second = 0;
        m_Variables.erase(v);
        v = m_Variables.begin();
    }

    m_InitialState = "";
    res = true;
} else
{
    iThrow(CRITICAL(FSM_CLEAR_STARTED));
}
return res;

}

Discussion


Log in to post a comment.