Fixed memory leak in MDLLoader.cpp

If one of the MDL importer implementations throw an exception, the memory allocated at mBuffer may never be freed. This fix should prevent further memory leaks.
pull/2927/head
Marc-Antoine Lortie 2020-01-18 14:58:19 -05:00
parent e0571329e7
commit 247667233d
1 changed files with 89 additions and 78 deletions

View File

@ -185,6 +185,17 @@ void MDLImporter::InternReadFile( const std::string& pFile,
throw DeadlyImportError( "MDL File is too small."); throw DeadlyImportError( "MDL File is too small.");
} }
// delete the file buffer and cleanup.
auto DeleteBufferAndCleanup = [&]() {
if (mBuffer) {
delete mBuffer;
mBuffer = nullptr;
}
AI_DEBUG_INVALIDATE_PTR(pIOHandler);
AI_DEBUG_INVALIDATE_PTR(pScene);
};
try {
// Allocate storage and copy the contents of the file to a memory buffer // Allocate storage and copy the contents of the file to a memory buffer
mBuffer = new unsigned char[iFileSize+1]; mBuffer = new unsigned char[iFileSize+1];
file->Read( (void*)mBuffer, 1, iFileSize); file->Read( (void*)mBuffer, 1, iFileSize);
@ -261,11 +272,11 @@ void MDLImporter::InternReadFile( const std::string& pFile,
pScene->mRootNode->mTransformation = aiMatrix4x4(1.f,0.f,0.f,0.f, pScene->mRootNode->mTransformation = aiMatrix4x4(1.f,0.f,0.f,0.f,
0.f,0.f,1.f,0.f,0.f,-1.f,0.f,0.f,0.f,0.f,0.f,1.f); 0.f,0.f,1.f,0.f,0.f,-1.f,0.f,0.f,0.f,0.f,0.f,1.f);
// delete the file buffer and cleanup DeleteBufferAndCleanup();
delete [] mBuffer; } catch(...) {
mBuffer= nullptr; DeleteBufferAndCleanup();
AI_DEBUG_INVALIDATE_PTR(pIOHandler); throw;
AI_DEBUG_INVALIDATE_PTR(pScene); }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------