Fixing memory leak in VAStruct destruction, now recursively clearing associated data

parent 5d397bed
......@@ -543,7 +543,7 @@ private:
int iValue; //!< Value if type is int
double dValue; //!< Value if type is double
std::string sValue; //!< Value if type is string
CVAStruct* xValue; //!< Value if type is struct
CVAStruct* pStruct; //!< Value if type is struct
std::vector< char > vcData; //!< Value if type is data
int iDataSize; //!< Value byte number if type is data
CVASampleBuffer sbValue; //!< Value if type is sample buffer
......
......@@ -30,6 +30,10 @@ CVAStruct::CVAStruct( const CVAStruct& rhs )
CVAStruct::~CVAStruct()
{
// Recursively destruct associated keys
CVAStruct::iterator it = Begin();
while( it != End() )
m_mData.erase( it++ );
}
int CVAStruct::Size() const
......@@ -270,17 +274,17 @@ std::string CVAStruct::ToString( const int iIndent ) const
// ----------------------------------------------------
// Constructors
CVAStructValue::CVAStructValue() : iDatatype( UNASSIGNED ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const bool value ) : iDatatype( BOOL ), bValue( value ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const int value ) : iDatatype( INT ), iValue( value ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const double value ) : iDatatype( DOUBLE ), dValue( value ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const char* value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const std::string& value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ) {};
CVAStructValue::CVAStructValue( const CVAStruct& value ) : iDatatype( STRUCT ), xValue( new CVAStruct( value ) ) {};
CVAStructValue::CVAStructValue() : iDatatype( UNASSIGNED ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const bool value ) : iDatatype( BOOL ), bValue( value ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const int value ) : iDatatype( INT ), iValue( value ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const double value ) : iDatatype( DOUBLE ), dValue( value ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const char* value ) : iDatatype( STRING ), sValue( value ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const std::string& value ) : iDatatype( STRING ), sValue( value ), pStruct( NULL ) {};
CVAStructValue::CVAStructValue( const CVAStruct& value ) : iDatatype( STRUCT ), pStruct( new CVAStruct( value ) ) {};
CVAStructValue::CVAStructValue( void* pData, const int iBytes )
: iDatatype( DATA )
, xValue( NULL )
, pStruct( NULL )
{
SetData( pData, iBytes );
}
......@@ -292,14 +296,14 @@ CVAStructValue::CVAStructValue( const CVASampleBuffer& oSampleBuffer )
CVAStructValue::CVAStructValue( const CVAStructValue& rhs )
: iDatatype( UNASSIGNED )
, xValue( NULL )
, pStruct( NULL )
{
*this = rhs;
}
CVAStructValue::~CVAStructValue()
{
delete xValue;
delete pStruct;
}
int CVAStructValue::GetDatatype() const
......@@ -365,16 +369,16 @@ const CVAStruct& CVAStructValue::GetStruct() const
{
if( iDatatype != STRUCT )
VA_EXCEPT1( "Key value is not a structure" );
assert( xValue );
return *xValue;
assert( pStruct );
return *pStruct;
}
CVAStruct& CVAStructValue::GetStruct()
{
if( iDatatype != STRUCT )
VA_EXCEPT1( "Key value is not a structure" );
assert( xValue );
return *xValue;
assert( pStruct );
return *pStruct;
}
bool CVAStructValue::IsAssigned() const
......@@ -428,8 +432,8 @@ CVAStructValue& CVAStructValue::operator=( const CVAStructValue& rhs )
if( this == &rhs )
return *this;
// Free data, if the key was of datatype DATA before
delete xValue;
// Free associated struct first
delete pStruct;
iDataSize = 0;
// Fast assignment. Copy only what is necessary ...
......@@ -454,7 +458,7 @@ CVAStructValue& CVAStructValue::operator=( const CVAStructValue& rhs )
case STRUCT:
// Autonome Kopie der Schssel Struktur erzeugen
xValue = new CVAStruct( *rhs.xValue );
pStruct = new CVAStruct( *rhs.pStruct );
break;
case DATA:
......@@ -516,14 +520,14 @@ const CVAStructValue& CVAStructValue::operator[]( const char* pcKey ) const
{
if( !IsStruct() )
VA_EXCEPT1( "Key is not a structure" );
return ( *xValue )[ pcKey ];
return ( *pStruct )[ pcKey ];
}
CVAStructValue& CVAStructValue::operator[]( const char* pcKey )
{
if( !IsStruct() )
VA_EXCEPT1( "Key is not a structure" );
return ( *xValue )[ pcKey ];
return ( *pStruct )[ pcKey ];
}
CVAStructValue::operator bool() const
......@@ -755,7 +759,7 @@ CVAStructValue::operator const CVAStruct&( ) const
VA_EXCEPT1( "Types cannot be converted" );
}
return *xValue;
return *pStruct;
}
CVAStructValue::operator CVAStruct&( )
......@@ -772,7 +776,7 @@ CVAStructValue::operator CVAStruct&( )
VA_EXCEPT1( "Types cannot be converted" );
}
return *xValue;
return *pStruct;
}
std::string CVAStructValue::ToString() const
......@@ -797,7 +801,7 @@ std::string CVAStructValue::ToString() const
return sValue;
case STRUCT:
return xValue->ToString();
return pStruct->ToString();
case SAMPLEBUFFER:
ss << sbValue.GetNumSamples();
......
......@@ -11,33 +11,52 @@
using namespace std;
int main() {
int main()
{
std::map<std::string, int> map;
try {
try
{
std::vector< char > vcSomeData( 1e7 );
vcSomeData[ 0 ] = 1;
vcSomeData[ vcSomeData.size() - 1 ] = -1;
{
CVAStruct s;
CVAStructValue v = CVAStructValue( &vcSomeData[ 0 ], sizeof( char ) * vcSomeData.size() );
s[ "ch1" ] = v;
}
for( size_t i = 0; i < 10e6; i++ )
{
CVAStruct s;
CVAStructValue v = CVAStructValue( &vcSomeData[ 0 ], sizeof( char ) * vcSomeData.size() );
s[ "ch1" ] = v;
}
CVAStruct s;
s["a"] = CVAStruct();
s[string("a")][string("b")] = 24;
s[string("a")][string("c")] = CVAStruct();
s[string("a")][string("c")][string("d")] = "Hui";
s[ "a" ] = CVAStruct();
s[ string( "a" ) ][ string( "b" ) ] = 24;
s[ string( "a" ) ][ string( "c" ) ] = CVAStruct();
s[ string( "a" ) ][ string( "c" ) ][ string( "d" ) ] = "Hui";
s.GetValue("a/c/d", '/');
s.GetValue( "a/c/d", '/' );
cout << s << endl;
CVASampleBuffer oBuf( 1024 );
oBuf.GetData()[0] = -1.0f;
oBuf.GetData()[1023] = 1023.0f;
CVAStructValue v( (void*) oBuf.GetData(), oBuf.GetNumSamples() * sizeof( float ) );
s["BRIR_S0_R0"] = v;
oBuf.GetData()[ 0 ] = -1.0f;
oBuf.GetData()[ 1023 ] = 1023.0f;
CVAStructValue v( ( void* ) oBuf.GetData(), oBuf.GetNumSamples() * sizeof( float ) );
s[ "BRIR_S0_R0" ] = v;
CVASampleBuffer oBuf2( 1024, false );
float* pvData = (float*)(void*) s["BRIR_S0_R0"];
float* pvData = ( float* ) ( void* ) s[ "BRIR_S0_R0" ];
for( size_t i=0; i< oBuf2.GetNumSamples(); i++ )
oBuf2.GetData()[i] = pvData[i];
for( size_t i = 0; i < oBuf2.GetNumSamples(); i++ )
oBuf2.GetData()[ i ] = pvData[ i ];
} catch(CVAException& e) {
}
catch( CVAException& e ) {
cerr << "Error: " << e << endl;
return e.GetErrorCode();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment