From 79a442f95ddc1be4828bd6c34f63d6369418e883 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Jan 2021 20:25:02 +0100 Subject: [PATCH] Fixes a mem leak in aiMetadata::Set --- include/assimp/metadata.h | 4 ++++ test/unit/utMetadata.cpp | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/assimp/metadata.h b/include/assimp/metadata.h index 52121fbf1..e44b97bf3 100644 --- a/include/assimp/metadata.h +++ b/include/assimp/metadata.h @@ -350,6 +350,10 @@ struct aiMetadata { } else if (nullptr != mValues[index].mData && AI_AIMETADATA == mValues[index].mType) { *static_cast(mValues[index].mData) = value; } else { + if (nullptr != mValues[index].mData) { + delete mValues[index].mData; + mValues[index].mData = nullptr; + } mValues[index].mData = new T(value); } diff --git a/test/unit/utMetadata.cpp b/test/unit/utMetadata.cpp index 81ab61435..d4b0b158f 100644 --- a/test/unit/utMetadata.cpp +++ b/test/unit/utMetadata.cpp @@ -51,11 +51,11 @@ class utMetadata: public ::testing::Test { protected: aiMetadata *m_data; - virtual void SetUp() { + void SetUp() override { m_data = nullptr; } - virtual void TearDown() { + void TearDown() override { aiMetadata::Dealloc( m_data ); } @@ -261,3 +261,12 @@ TEST_F( utMetadata, copy_test ) { EXPECT_EQ( metaVal, v ); } } + +TEST_F( utMetadata, set_test ) { + aiMetadata v; + const std::string key_bool = "test_bool"; + v.Set(1, key_bool, true); + v.Set(1, key_bool, true); + v.Set(1, key_bool, true); + v.Set(1, key_bool, true); +}