Fix invalid release of mat + mesh.
authorKim Kulling <kim.kulling@googlemail.com>
Sun, 4 Sep 2016 18:22:04 +0000 (20:22 +0200)
committerKim Kulling <kim.kulling@googlemail.com>
Sun, 4 Sep 2016 18:22:04 +0000 (20:22 +0200)
code/IRRMeshLoader.cpp

index 0a2a54e..567c981 100644 (file)
@@ -116,6 +116,16 @@ const aiImporterDesc* IRRMeshImporter::GetInfo () const
     return &desc;
 }
 
+static void releaseMaterial( aiMaterial **mat ) {
+    delete mat;
+    *mat = nullptr;
+}
+
+static void releaseMesh( aiMesh **mesh ) {
+    delete mesh;
+    *mesh = nullptr;
+}
+
 // ------------------------------------------------------------------------------------------------
 // Imports the given file into the given scene structure.
 void IRRMeshImporter::InternReadFile( const std::string& pFile,
@@ -135,7 +145,7 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
     std::vector<aiMaterial*> materials;
     std::vector<aiMesh*>     meshes;
     materials.reserve (5);
-    meshes.reserve    (5);
+    meshes.reserve(5);
 
     // temporary data - current mesh buffer
     aiMaterial* curMat  = NULL;
@@ -159,11 +169,10 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
             if (!ASSIMP_stricmp(reader->getNodeName(),"buffer") && (curMat || curMesh)) {
                 // end of previous buffer. A material and a mesh should be there
                 if ( !curMat || !curMesh)   {
-                    DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
-                    delete curMat;
-                    delete curMesh;
-                }
-                else    {
+                    DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");                    
+                    releaseMaterial( &curMat );
+                    releaseMesh( &curMesh );
+                } else {
                     materials.push_back(curMat);
                     meshes.push_back(curMesh);
                 }
@@ -183,7 +192,7 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
             if (!ASSIMP_stricmp(reader->getNodeName(),"material"))  {
                 if (curMat) {
                     DefaultLogger::get()->warn("IRRMESH: Only one material description per buffer, please");
-                    delete curMat;curMat = NULL;
+                    releaseMaterial( &curMat );
                 }
                 curMat = ParseMaterial(curMatFlags);
             }
@@ -195,17 +204,16 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
                     // This is possible ... remove the mesh from the list and skip further reading
                     DefaultLogger::get()->warn("IRRMESH: Found mesh with zero vertices");
 
-                    delete curMat;curMat = NULL;
-
-                    curMesh = NULL;
+                    releaseMaterial( &curMat );
+                    releaseMesh( &curMesh );
                     textMeaning = 0;
                     continue;
                 }
 
-                curVertices.reserve (num);
-                curNormals.reserve  (num);
-                curColors.reserve   (num);
-                curUVs.reserve      (num);
+                curVertices.reserve(num);
+                curNormals.reserve(num);
+                curColors.reserve(num);
+                curUVs.reserve(num);
 
                 // Determine the file format
                 const char* t = reader->getAttributeValueSafe("type");
@@ -240,7 +248,7 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
                     vertexFormat = 2;
                 }
                 else if (ASSIMP_stricmp("standard", t)) {
-                    delete curMat;
+                    releaseMaterial( &curMat );
                     DefaultLogger::get()->warn("IRRMESH: Unknown vertex format");
                 }
                 else vertexFormat = 0;
@@ -248,7 +256,7 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
             }
             else if (!ASSIMP_stricmp(reader->getNodeName(),"indices"))  {
                 if (curVertices.empty() && curMat)  {
-                    delete curMat;
+                    releaseMaterial( &curMat );
                     throw DeadlyImportError("IRRMESH: indices must come after vertices");
                 }
 
@@ -264,10 +272,10 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
                     DefaultLogger::get()->warn("IRRMESH: Found mesh with zero indices");
 
                     // mesh - away
-                    delete curMesh; curMesh = NULL;
+                    releaseMesh( &curMesh );
 
                     // material - away
-                    delete curMat;curMat = NULL;
+                    releaseMaterial( &curMat );
 
                     textMeaning = 0;
                     continue;
@@ -469,7 +477,6 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
             break;
 
             default:
-
                 // GCC complains here ...
                 break;
 
@@ -480,8 +487,8 @@ void IRRMeshImporter::InternReadFile( const std::string& pFile,
     if (curMat || curMesh)  {
         if ( !curMat || !curMesh)   {
             DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
-            delete curMat;
-            delete curMesh;
+            releaseMaterial( &curMat );
+            releaseMesh( &curMesh );
         }
         else    {
             materials.push_back(curMat);