瀏覽代碼

BlenderLoader: Fix memory leak

Turo Lamminen 7 年之前
父節點
當前提交
c42dd9104c
共有 1 個文件被更改,包括 5 次插入13 次删除
  1. 5 13
      code/BlenderLoader.cpp

+ 5 - 13
code/BlenderLoader.cpp

@@ -154,14 +154,6 @@ void BlenderImporter::SetupProperties(const Importer* /*pImp*/)
     // nothing to be done for the moment
     // nothing to be done for the moment
 }
 }
 
 
-struct free_it {
-    free_it(void* free) : free(free) {}
-    ~free_it() {
-        ::free(this->free);
-    }
-
-    void* free;
-};
 
 
 // ------------------------------------------------------------------------------------------------
 // ------------------------------------------------------------------------------------------------
 // Imports the given file into the given scene structure.
 // Imports the given file into the given scene structure.
@@ -169,8 +161,7 @@ void BlenderImporter::InternReadFile( const std::string& pFile,
     aiScene* pScene, IOSystem* pIOHandler)
     aiScene* pScene, IOSystem* pIOHandler)
 {
 {
 #ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
 #ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
-    Bytef* dest = NULL;
-    free_it free_it_really(dest);
+    std::vector<Bytef> uncompressed;
 #endif
 #endif
 
 
 
 
@@ -218,6 +209,7 @@ void BlenderImporter::InternReadFile( const std::string& pFile,
 
 
         size_t total = 0l;
         size_t total = 0l;
 
 
+        // TODO: be smarter about this, decompress directly into heap buffer
         // and decompress the data .... do 1k chunks in the hope that we won't kill the stack
         // and decompress the data .... do 1k chunks in the hope that we won't kill the stack
 #define MYBLOCK 1024
 #define MYBLOCK 1024
         Bytef block[MYBLOCK];
         Bytef block[MYBLOCK];
@@ -232,8 +224,8 @@ void BlenderImporter::InternReadFile( const std::string& pFile,
             }
             }
             const size_t have = MYBLOCK - zstream.avail_out;
             const size_t have = MYBLOCK - zstream.avail_out;
             total += have;
             total += have;
-            dest = reinterpret_cast<Bytef*>( realloc(dest,total) );
-            memcpy(dest + total - have,block,have);
+            uncompressed.resize(total);
+            memcpy(uncompressed.data() + total - have,block,have);
         }
         }
         while (ret != Z_STREAM_END);
         while (ret != Z_STREAM_END);
 
 
@@ -241,7 +233,7 @@ void BlenderImporter::InternReadFile( const std::string& pFile,
         inflateEnd(&zstream);
         inflateEnd(&zstream);
 
 
         // replace the input stream with a memory stream
         // replace the input stream with a memory stream
-        stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t*>(dest),total));
+        stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t*>(uncompressed.data()),total));
 
 
         // .. and retry
         // .. and retry
         stream->Read(magic,7,1);
         stream->Read(magic,7,1);