XGLLoader: Fix a memory leak

pull/1775/head
Turo Lamminen 2018-02-06 18:43:51 +02:00
parent 48d41b4bf9
commit d284d107e7
1 changed files with 5 additions and 16 deletions

View File

@ -72,17 +72,6 @@ using namespace irr::io;
#endif #endif
// scopeguard for a malloc'ed buffer
struct free_it
{
free_it(void* free) : free(free) {}
~free_it() {
::free(this->free);
}
void* free;
};
namespace Assimp { // this has to be in here because LogFunctions is in ::Assimp namespace Assimp { // this has to be in here because LogFunctions is in ::Assimp
template<> const char* LogFunctions<XGLImporter>::Prefix() template<> const char* LogFunctions<XGLImporter>::Prefix()
{ {
@ -155,8 +144,7 @@ void XGLImporter::InternReadFile( const std::string& pFile,
aiScene* pScene, IOSystem* pIOHandler) aiScene* pScene, IOSystem* pIOHandler)
{ {
#ifndef ASSIMP_BUILD_NO_COMPRESSED_XGL #ifndef ASSIMP_BUILD_NO_COMPRESSED_XGL
Bytef* dest = NULL; std::vector<Bytef> uncompressed;
free_it free_it_really(dest);
#endif #endif
m_scene = pScene; m_scene = pScene;
@ -192,6 +180,7 @@ void XGLImporter::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];
@ -206,8 +195,8 @@ void XGLImporter::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) ); uncompressed.resize(total);
memcpy(dest + total - have,block,have); memcpy(uncompressed.data() + total - have,block,have);
} }
while (ret != Z_STREAM_END); while (ret != Z_STREAM_END);
@ -215,7 +204,7 @@ void XGLImporter::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));
#endif #endif
} }