fixes potential memory leak on malformed obj file (#5645)
parent
d0703a5a3a
commit
0b19b7d73b
|
@ -193,7 +193,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
|
||||||
pScene->mRootNode->mChildren = new aiNode *[childCount];
|
pScene->mRootNode->mChildren = new aiNode *[childCount];
|
||||||
|
|
||||||
// Create nodes for the whole scene
|
// Create nodes for the whole scene
|
||||||
std::vector<aiMesh *> MeshArray;
|
std::vector<std::unique_ptr<aiMesh>> MeshArray;
|
||||||
MeshArray.reserve(meshCount);
|
MeshArray.reserve(meshCount);
|
||||||
for (size_t index = 0; index < pModel->mObjects.size(); ++index) {
|
for (size_t index = 0; index < pModel->mObjects.size(); ++index) {
|
||||||
createNodes(pModel, pModel->mObjects[index], pScene->mRootNode, pScene, MeshArray);
|
createNodes(pModel, pModel->mObjects[index], pScene->mRootNode, pScene, MeshArray);
|
||||||
|
@ -205,7 +205,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
|
||||||
if (pScene->mNumMeshes > 0) {
|
if (pScene->mNumMeshes > 0) {
|
||||||
pScene->mMeshes = new aiMesh *[MeshArray.size()];
|
pScene->mMeshes = new aiMesh *[MeshArray.size()];
|
||||||
for (size_t index = 0; index < MeshArray.size(); ++index) {
|
for (size_t index = 0; index < MeshArray.size(); ++index) {
|
||||||
pScene->mMeshes[index] = MeshArray[index];
|
pScene->mMeshes[index] = MeshArray[index].release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +257,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
|
||||||
// Creates all nodes of the model
|
// Creates all nodes of the model
|
||||||
aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
|
aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
|
||||||
aiNode *pParent, aiScene *pScene,
|
aiNode *pParent, aiScene *pScene,
|
||||||
std::vector<aiMesh *> &MeshArray) {
|
std::vector<std::unique_ptr<aiMesh>> &MeshArray) {
|
||||||
ai_assert(nullptr != pModel);
|
ai_assert(nullptr != pModel);
|
||||||
if (nullptr == pObject) {
|
if (nullptr == pObject) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -275,12 +275,10 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile
|
||||||
|
|
||||||
for (size_t i = 0; i < pObject->m_Meshes.size(); ++i) {
|
for (size_t i = 0; i < pObject->m_Meshes.size(); ++i) {
|
||||||
unsigned int meshId = pObject->m_Meshes[i];
|
unsigned int meshId = pObject->m_Meshes[i];
|
||||||
aiMesh *pMesh = createTopology(pModel, pObject, meshId);
|
std::unique_ptr<aiMesh> pMesh = createTopology(pModel, pObject, meshId);
|
||||||
if (pMesh != nullptr) {
|
if (pMesh != nullptr) {
|
||||||
if (pMesh->mNumFaces > 0) {
|
if (pMesh->mNumFaces > 0) {
|
||||||
MeshArray.push_back(pMesh);
|
MeshArray.push_back(std::move(pMesh));
|
||||||
} else {
|
|
||||||
delete pMesh;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,7 +310,7 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// Create topology data
|
// Create topology data
|
||||||
aiMesh *ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
|
std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
|
||||||
// Checking preconditions
|
// Checking preconditions
|
||||||
ai_assert(nullptr != pModel);
|
ai_assert(nullptr != pModel);
|
||||||
|
|
||||||
|
@ -394,7 +392,7 @@ aiMesh *ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjF
|
||||||
// Create mesh vertices
|
// Create mesh vertices
|
||||||
createVertexArray(pModel, pData, meshIndex, pMesh.get(), uiIdxCount);
|
createVertexArray(pModel, pData, meshIndex, pMesh.get(), uiIdxCount);
|
||||||
|
|
||||||
return pMesh.release();
|
return pMesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
|
@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#include <assimp/BaseImporter.h>
|
#include <assimp/BaseImporter.h>
|
||||||
#include <assimp/material.h>
|
#include <assimp/material.h>
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct aiMesh;
|
struct aiMesh;
|
||||||
|
@ -84,10 +85,10 @@ protected:
|
||||||
|
|
||||||
//! \brief Creates all nodes stored in imported content.
|
//! \brief Creates all nodes stored in imported content.
|
||||||
aiNode *createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pData,
|
aiNode *createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pData,
|
||||||
aiNode *pParent, aiScene *pScene, std::vector<aiMesh *> &MeshArray);
|
aiNode *pParent, aiScene *pScene, std::vector<std::unique_ptr<aiMesh>> &MeshArray);
|
||||||
|
|
||||||
//! \brief Creates topology data like faces and meshes for the geometry.
|
//! \brief Creates topology data like faces and meshes for the geometry.
|
||||||
aiMesh *createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData,
|
std::unique_ptr<aiMesh> createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData,
|
||||||
unsigned int uiMeshIndex);
|
unsigned int uiMeshIndex);
|
||||||
|
|
||||||
//! \brief Creates vertices from model.
|
//! \brief Creates vertices from model.
|
||||||
|
|
Loading…
Reference in New Issue