From 12f184867e514e393c9e4eaa78d8cf421759476e Mon Sep 17 00:00:00 2001 From: Mike Samsonov Date: Tue, 19 Nov 2019 17:05:24 +0000 Subject: [PATCH 1/2] Fix for memory leak in glTF2 Importer if an exception has been thrown --- code/glTF2/glTF2Asset.inl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/code/glTF2/glTF2Asset.inl b/code/glTF2/glTF2Asset.inl index 6b47b1607..0d4de11f2 100644 --- a/code/glTF2/glTF2Asset.inl +++ b/code/glTF2/glTF2Asset.inl @@ -270,13 +270,14 @@ Ref LazyDict::Retrieve(unsigned int i) throw DeadlyImportError("GLTF: Object at index \"" + to_string(i) + "\" is not a JSON object"); } - T* inst = new T(); + // Unique ptr prevents memory leak in case of Read throws an exception + auto inst = std::unique_ptr(); inst->id = std::string(mDictId) + "_" + to_string(i); inst->oIndex = i; ReadMember(obj, "name", inst->name); inst->Read(obj, mAsset); - return Add(inst); + return Add(inst.release()); } template From 91af4b7476fb9ecd86cdf1c7ae0829aa4e219dc1 Mon Sep 17 00:00:00 2001 From: Mike Samsonov Date: Tue, 19 Nov 2019 17:43:31 +0000 Subject: [PATCH 2/2] fix the crash --- code/glTF2/glTF2Asset.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/glTF2/glTF2Asset.inl b/code/glTF2/glTF2Asset.inl index 0d4de11f2..4cd0f65c0 100644 --- a/code/glTF2/glTF2Asset.inl +++ b/code/glTF2/glTF2Asset.inl @@ -271,7 +271,7 @@ Ref LazyDict::Retrieve(unsigned int i) } // Unique ptr prevents memory leak in case of Read throws an exception - auto inst = std::unique_ptr(); + auto inst = std::unique_ptr(new T()); inst->id = std::string(mDictId) + "_" + to_string(i); inst->oIndex = i; ReadMember(obj, "name", inst->name);