From 4af9632269568644bb881a9b423740293331db02 Mon Sep 17 00:00:00 2001 From: wise86Android Date: Sat, 16 Jan 2016 22:51:18 +0100 Subject: [PATCH] fix memory leak --- code/MaterialSystem.cpp | 8 +-- code/MemoryIOWrapper.h | 3 +- code/RemoveVCProcess.cpp | 2 +- test/unit/utRemoveRedundantMaterials.cpp | 1 - test/unit/utSharedPPData.cpp | 34 +++++----- test/unit/utSplitLargeMeshes.cpp | 81 ++++++++++++------------ 6 files changed, 65 insertions(+), 64 deletions(-) diff --git a/code/MaterialSystem.cpp b/code/MaterialSystem.cpp index fe5559148..02f971737 100644 --- a/code/MaterialSystem.cpp +++ b/code/MaterialSystem.cpp @@ -53,7 +53,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../include/assimp/DefaultLogger.hpp" #include "Macros.h" - using namespace Assimp; // ------------------------------------------------------------------------------------------------ @@ -571,9 +570,11 @@ void aiMaterial::CopyPropertyList(aiMaterial* pcDest, for (unsigned int i = 0; i < iOldNum;++i) { pcDest->mProperties[i] = pcOld[i]; } - - delete[] pcOld; } + + if(pcOld) + delete[] pcOld; + for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i) { aiMaterialProperty* propSrc = pcSrc->mProperties[i]; @@ -605,4 +606,3 @@ void aiMaterial::CopyPropertyList(aiMaterial* pcDest, } return; } - diff --git a/code/MemoryIOWrapper.h b/code/MemoryIOWrapper.h index 39791450a..acd6d8633 100644 --- a/code/MemoryIOWrapper.h +++ b/code/MemoryIOWrapper.h @@ -178,7 +178,8 @@ public: // ------------------------------------------------------------------- /** Closes the given file and releases all resources associated with it. */ - void Close( IOStream* /*pFile*/) { + void Close( IOStream* pFile) { + delete pFile; } // ------------------------------------------------------------------- diff --git a/code/RemoveVCProcess.cpp b/code/RemoveVCProcess.cpp index 17af6f961..17ad5ff33 100644 --- a/code/RemoveVCProcess.cpp +++ b/code/RemoveVCProcess.cpp @@ -283,7 +283,7 @@ bool RemoveVCProcess::ProcessMesh(aiMesh* pMesh) if (!pMesh->mTextureCoords[i])break; if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b) { - delete pMesh->mTextureCoords[i]; + delete [] pMesh->mTextureCoords[i]; pMesh->mTextureCoords[i] = NULL; ret = true; diff --git a/test/unit/utRemoveRedundantMaterials.cpp b/test/unit/utRemoveRedundantMaterials.cpp index 0b2382261..e660a34fa 100644 --- a/test/unit/utRemoveRedundantMaterials.cpp +++ b/test/unit/utRemoveRedundantMaterials.cpp @@ -60,7 +60,6 @@ protected: RemoveRedundantMatsProcess* piProcess; aiScene* pcScene1; - aiScene* pcScene2; }; // ------------------------------------------------------------------------------------------------ diff --git a/test/unit/utSharedPPData.cpp b/test/unit/utSharedPPData.cpp index 07c137287..73172166c 100644 --- a/test/unit/utSharedPPData.cpp +++ b/test/unit/utSharedPPData.cpp @@ -59,28 +59,16 @@ protected: SharedPostProcessInfo* shared; }; -static bool destructed; - -struct TestType -{ - ~TestType() - { - destructed = true; - } -}; - - // ------------------------------------------------------------------------------------------------ void SharedPPDataTest::SetUp() { shared = new SharedPostProcessInfo(); - destructed = false; } // ------------------------------------------------------------------------------------------------ void SharedPPDataTest::TearDown() { - + delete shared; } // ------------------------------------------------------------------------------------------------ @@ -112,14 +100,26 @@ TEST_F(SharedPPDataTest, testPropertyPointer) EXPECT_FALSE(shared->GetProperty("test16",o)); } +static bool destructed; + +struct TestType +{ + ~TestType() + { + destructed = true; + } +}; // ------------------------------------------------------------------------------------------------ TEST_F(SharedPPDataTest, testPropertyDeallocation) { - TestType *out, * pip = new TestType(); - shared->AddProperty("quak",pip); - EXPECT_TRUE(shared->GetProperty("quak",out)); + SharedPostProcessInfo* localShared = new SharedPostProcessInfo(); + destructed = false; + + TestType *out, * pip = new TestType(); + localShared->AddProperty("quak",pip); + EXPECT_TRUE(localShared->GetProperty("quak",out)); EXPECT_EQ(pip, out); - delete shared; + delete localShared; EXPECT_TRUE(destructed); } diff --git a/test/unit/utSplitLargeMeshes.cpp b/test/unit/utSplitLargeMeshes.cpp index 02a8fe556..0814f2932 100644 --- a/test/unit/utSplitLargeMeshes.cpp +++ b/test/unit/utSplitLargeMeshes.cpp @@ -58,8 +58,7 @@ protected: SplitLargeMeshesProcess_Triangle* piProcessTriangle; SplitLargeMeshesProcess_Vertex* piProcessVertex; - aiMesh* pcMesh1; - aiMesh* pcMesh2; + }; // ------------------------------------------------------------------------------------------------ @@ -72,44 +71,6 @@ void SplitLargeMeshesTest::SetUp() this->piProcessTriangle->SetLimit(1000); this->piProcessVertex->SetLimit(1000); - this->pcMesh1 = new aiMesh(); - pcMesh1->mNumVertices = 2100; // quersumme: 3 - pcMesh1->mVertices = new aiVector3D[pcMesh1->mNumVertices]; - pcMesh1->mNormals = new aiVector3D[pcMesh1->mNumVertices]; - - pcMesh1->mNumFaces = pcMesh1->mNumVertices / 3; - pcMesh1->mFaces = new aiFace[pcMesh1->mNumFaces]; - - unsigned int qq = 0; - for (unsigned int i = 0; i < pcMesh1->mNumFaces;++i) - { - aiFace& face = pcMesh1->mFaces[i]; - face.mNumIndices = 3; - face.mIndices = new unsigned int[3]; - face.mIndices[0] = qq++; - face.mIndices[1] = qq++; - face.mIndices[2] = qq++; - } - - // generate many, many faces with randomized indices for - // the second mesh - this->pcMesh2 = new aiMesh(); - pcMesh2->mNumVertices = 3000; - pcMesh2->mVertices = new aiVector3D[pcMesh2->mNumVertices]; - pcMesh2->mNormals = new aiVector3D[pcMesh2->mNumVertices]; - - pcMesh2->mNumFaces = 10000; - pcMesh2->mFaces = new aiFace[pcMesh2->mNumFaces]; - - for (unsigned int i = 0; i < pcMesh2->mNumFaces;++i) - { - aiFace& face = pcMesh2->mFaces[i]; - face.mNumIndices = 3; - face.mIndices = new unsigned int[3]; - face.mIndices[0] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); - face.mIndices[1] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); - face.mIndices[2] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); - } } // ------------------------------------------------------------------------------------------------ @@ -124,6 +85,26 @@ TEST_F(SplitLargeMeshesTest, testVertexSplit) { std::vector< std::pair > avOut; + aiMesh *pcMesh1 = new aiMesh(); + pcMesh1->mNumVertices = 2100; // quersumme: 3 + pcMesh1->mVertices = new aiVector3D[pcMesh1->mNumVertices]; + pcMesh1->mNormals = new aiVector3D[pcMesh1->mNumVertices]; + + pcMesh1->mNumFaces = pcMesh1->mNumVertices / 3; + pcMesh1->mFaces = new aiFace[pcMesh1->mNumFaces]; + + unsigned int qq = 0; + for (unsigned int i = 0; i < pcMesh1->mNumFaces;++i) + { + aiFace& face = pcMesh1->mFaces[i]; + face.mNumIndices = 3; + face.mIndices = new unsigned int[3]; + face.mIndices[0] = qq++; + face.mIndices[1] = qq++; + face.mIndices[2] = qq++; + } + + int iOldFaceNum = (int)pcMesh1->mNumFaces; piProcessVertex->SplitMesh(0,pcMesh1,avOut); @@ -147,6 +128,26 @@ TEST_F(SplitLargeMeshesTest, testTriangleSplit) { std::vector< std::pair > avOut; + // generate many, many faces with randomized indices for + // the second mesh + aiMesh *pcMesh2 = new aiMesh(); + pcMesh2->mNumVertices = 3000; + pcMesh2->mVertices = new aiVector3D[pcMesh2->mNumVertices]; + pcMesh2->mNormals = new aiVector3D[pcMesh2->mNumVertices]; + + pcMesh2->mNumFaces = 10000; + pcMesh2->mFaces = new aiFace[pcMesh2->mNumFaces]; + + for (unsigned int i = 0; i < pcMesh2->mNumFaces;++i) + { + aiFace& face = pcMesh2->mFaces[i]; + face.mNumIndices = 3; + face.mIndices = new unsigned int[3]; + face.mIndices[0] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); + face.mIndices[1] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); + face.mIndices[2] = (unsigned int)((rand() / (float)RAND_MAX) * pcMesh2->mNumVertices); + } + // the number of faces shouldn't change int iOldFaceNum = (int)pcMesh2->mNumFaces; piProcessTriangle->SplitMesh(0,pcMesh2,avOut);