summaryrefslogtreecommitdiffstats
path: root/src/plugins/sceneparsers
diff options
context:
space:
mode:
authorMäättä Antti <antti.maatta@qt.io>2017-07-19 09:56:35 +0300
committerAntti Määttä <antti.maatta@qt.io>2017-07-20 06:31:04 +0000
commit102fef33f14f21869725ebfa46d57c31ec4002b3 (patch)
tree0dfc6d017006f8f9a0000263f334acf5d0367ceb /src/plugins/sceneparsers
parentd82620680292f40af522317bf360e48b4e1c7990 (diff)
Fix memory leaks in assimp SceneLoader
Add deallocation guard agains resources allocated by the assimpimporter and deallocate them when the scene is deallocated. Task-number: QTBUG-61856 Change-Id: I2f7ddb1654da1674fa448861dfd2b368a55eb4f0 Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'src/plugins/sceneparsers')
-rw-r--r--src/plugins/sceneparsers/assimp/assimpimporter.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/plugins/sceneparsers/assimp/assimpimporter.cpp b/src/plugins/sceneparsers/assimp/assimpimporter.cpp
index a76f09a69..15a080ca7 100644
--- a/src/plugins/sceneparsers/assimp/assimpimporter.cpp
+++ b/src/plugins/sceneparsers/assimp/assimpimporter.cpp
@@ -657,6 +657,13 @@ void AssimpImporter::loadMaterial(uint materialIndex)
copyMaterialTextures(material, assimpMaterial);
m_scene->m_materials.insert(materialIndex, material);
+
+ Qt3DRender::AssimpImporter::SceneImporter *scene = m_scene;
+ QObject::connect(material, &QObject::destroyed, [&, scene, materialIndex](QObject *object) {
+ QMaterial *r = static_cast<QMaterial *>(object);
+ if (scene->m_materials[materialIndex] == r)
+ scene->m_materials.remove(materialIndex);
+ });
}
/*!
@@ -808,6 +815,13 @@ void AssimpImporter::loadMesh(uint meshIndex)
m_scene->m_meshes[meshIndex] = geometryRenderer;
+ Qt3DRender::AssimpImporter::SceneImporter *scene = m_scene;
+ QObject::connect(geometryRenderer, &QObject::destroyed, [&, scene, meshIndex](QObject *object) {
+ QGeometryRenderer *r = static_cast<QGeometryRenderer *>(object);
+ if (scene->m_meshes[meshIndex] == r)
+ scene->m_meshes.remove(meshIndex);
+ });
+
if (mesh->mNumAnimMeshes > 0) {
aiAnimMesh *animesh = mesh->mAnimMeshes[0];
@@ -968,6 +982,13 @@ void AssimpImporter::loadEmbeddedTexture(uint textureIndex)
imageData->setData(textureContent);
texture->addTextureImage(imageData);
m_scene->m_embeddedTextures[textureIndex] = texture;
+
+ Qt3DRender::AssimpImporter::SceneImporter *scene = m_scene;
+ QObject::connect(texture, &QObject::destroyed, [&, scene, textureIndex](QObject *object) {
+ QAbstractTexture *r = static_cast<QAbstractTexture *>(object);
+ if (scene->m_embeddedTextures[textureIndex] == r)
+ scene->m_embeddedTextures.remove(textureIndex);
+ });
}
/*!
@@ -1011,6 +1032,13 @@ void AssimpImporter::loadCamera(uint cameraIndex)
camera->addComponent(transform);
m_scene->m_cameras[cameraNode] = camera;
+
+ Qt3DRender::AssimpImporter::SceneImporter *scene = m_scene;
+ QObject::connect(camera, &QObject::destroyed, [&, scene, cameraNode](QObject *object) {
+ QEntity *r = static_cast<QEntity *>(object);
+ if (scene->m_cameras[cameraNode] == r)
+ scene->m_cameras.remove(cameraNode);
+ });
}
int findTimeIndex(const QVector<float> &times, float time) {
@@ -1341,9 +1369,15 @@ AssimpImporter::SceneImporter::SceneImporter()
// The Assimp::Importer manages the lifetime of the aiScene object
}
+
+
AssimpImporter::SceneImporter::~SceneImporter()
{
delete m_importer;
+ qDeleteAll(m_materials.values());
+ qDeleteAll(m_meshes.values());
+ qDeleteAll(m_embeddedTextures.values());
+ qDeleteAll(m_cameras.values());
}
} // namespace Qt3DRender