Menu

Solving memory leak in classes inherited from `AbsFunctionComponent`

Help
2013-11-06
2013-11-07
  • Suraj Krishnaswami

    Hi,
    Nearly all the classes inherited from 'AbsFunctionComponent' have the following comment.

    // BUG - These can not currenrtly be deleted.
    // The code is not consistant here. In places a new vector is returned
    // in others a reference to a vector that is managed by other code is returned.

    This memory leak can be solved using the following change in file ObjectComponent.cpp.
    The changed function is as follows,

    VariableValueVector* ObjectComponent::GetVariableValues() 
    {
        VariableValueVector* values = NULL;
        if(AbsDataCollector::GetIsRunning()) 
        {
            values = new VariableValueVector();
            CollectedObject* collectedObject = AbsObjectCollector::Instance()->Run(this->GetObjectId());
            *values = *(collectedObject->GetVariableValues());
        } else {
            values = ObjectReader::GetVariableValuesForObject(this->GetObjectId());
        }
    
        return values;
    }
    

    After making this change if we un-comment the code below,
    // BUG - These can not currenrtly be deleted.
    // The code is not consistant here. In places a new vector is returned
    // in others a reference to a vector that is managed by other code is returned.
    // delete tmp;
    // tmp = NULL;
    The interpreter works, and the result is also not changed, at least for the definition file that i had given.
    But is the above change valid, or will it cause some bugs or crashes in the interpreter.
    Please reply.

    Thanks & Regards,
    Suraj K.

     
  • Michael Chisholm

    I've added issue #265 for this, and I will look into it more thoroughly when I can.

    Thanks for the contribution,
    Andy

     
  • Suraj Krishnaswami

    Thankyou.

     

Log in to post a comment.