[Insight-developers] itkVariableLengthVector

Brad Davis brad.davis at kitware.com
Tue Aug 15 11:36:23 EDT 2006


I seem to have found a memory bug in VariableLengthVector.

The SetSize method needs to add the line commented below.  Otherwise,
m_Data is incorrectly accessed when the Reserve method is called.  I
have tested the change with the current cvs version of Insight and all
tests pass (Linux-x86_­64, gcc 4.0.4).

 template < typename TValueType >
void VariableLengthVector<TValueType >
::SetSize(unsigned int sz, bool destroyExistingData)
{
  if( destroyExistingData )
    {
    // Free any existing data if we manage its memory and if we need to destroy
    if(!m_LetArrayManageMemory)
      {
      m_Data=0;
      m_NumElements = 0;
      }
    else if( m_Data )
      {
      if( (m_NumElements != sz))
        {
        if(m_NumElements>0)
          {
          delete [] m_Data;
          m_Data = 0;  // THIS LINE MUST BE ADDED
          }
        }
      else return;
      }
    }

  if ( m_NumElements != sz )
    {
    Reserve( sz );
    }
}


More information about the Insight-developers mailing list