From 30ae14fae9ca2d2a2aaa185395c75e615518af87 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 18:40:28 +0200 Subject: [PATCH 1/6] B3DImporter: Add unique_to_array helper function --- code/B3DImporter.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index bc888fb66..46008bf31 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -267,6 +267,21 @@ T *B3DImporter::to_array( const vector &v ){ return p; } + +// ------------------------------------------------------------------------------------------------ +template +T **unique_to_array( vector > &v ){ + if( v.empty() ) { + return 0; + } + T **p = new T*[ v.size() ]; + for( size_t i = 0; i < v.size(); ++i ){ + p[i] = v[i].release(); + } + return p; +} + + // ------------------------------------------------------------------------------------------------ void B3DImporter::ReadTEXS(){ while( ChunkSize() ){ From acab4c327e53d9cdd468536861a5f5575ff7ae51 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 18:42:14 +0200 Subject: [PATCH 2/6] B3DImporter: Store animations in unique_ptr --- code/B3DImporter.cpp | 10 ++++------ code/B3DImporter.h | 3 ++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index 46008bf31..b3a01c79c 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -93,7 +93,6 @@ void DeleteAllBarePointers(std::vector& x) B3DImporter::~B3DImporter() { - DeleteAllBarePointers(_animations); } // ------------------------------------------------------------------------------------------------ @@ -515,11 +514,11 @@ void B3DImporter::ReadANIM(){ int frames=ReadInt(); float fps=ReadFloat(); - aiAnimation *anim=new aiAnimation; - _animations.push_back( anim ); + std::unique_ptr anim(new aiAnimation); anim->mDuration=frames; anim->mTicksPerSecond=fps; + _animations.emplace_back( std::move(anim) ); } // ------------------------------------------------------------------------------------------------ @@ -601,7 +600,6 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ _nodeAnims.clear(); - DeleteAllBarePointers(_animations); _animations.clear(); string t=ReadChunk(); @@ -715,12 +713,12 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ //animations if( _animations.size()==1 && _nodeAnims.size() ){ - aiAnimation *anim=_animations.back(); + aiAnimation *anim = _animations.back().get(); anim->mNumChannels=static_cast(_nodeAnims.size()); anim->mChannels=to_array( _nodeAnims ); scene->mNumAnimations=static_cast(_animations.size()); - scene->mAnimations=to_array( _animations ); + scene->mAnimations=unique_to_array( _animations ); } // convert to RH diff --git a/code/B3DImporter.h b/code/B3DImporter.h index 94644edd4..b283cfab6 100644 --- a/code/B3DImporter.h +++ b/code/B3DImporter.h @@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include "BaseImporter.h" +#include #include struct aiNodeAnim; @@ -124,7 +125,7 @@ private: std::vector _nodes; std::vector _meshes; std::vector _nodeAnims; - std::vector _animations; + std::vector > _animations; }; } From f1707e920d9b0534757326dd5786e44774eae6a3 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 18:54:41 +0200 Subject: [PATCH 3/6] B3DImporter: Store meshes in unique_ptr --- code/B3DImporter.cpp | 9 +++++---- code/B3DImporter.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index b3a01c79c..d764bd530 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -400,8 +400,7 @@ void B3DImporter::ReadTRIS( int v0 ){ Fail( "Bad material id" ); } - aiMesh *mesh=new aiMesh; - _meshes.push_back( mesh ); + std::unique_ptr mesh(new aiMesh); mesh->mMaterialIndex=matid; mesh->mNumFaces=0; @@ -429,6 +428,8 @@ void B3DImporter::ReadTRIS( int v0 ){ ++mesh->mNumFaces; ++face; } + + _meshes.emplace_back( std::move(mesh) ); } // ------------------------------------------------------------------------------------------------ @@ -635,7 +636,7 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ aiNode *node=_nodes[i]; for( size_t j=0;jmNumMeshes;++j ){ - aiMesh *mesh=_meshes[node->mMeshes[j]]; + aiMesh *mesh = _meshes[node->mMeshes[j]].get(); int n_tris=mesh->mNumFaces; int n_verts=mesh->mNumVertices=n_tris * 3; @@ -708,7 +709,7 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ //meshes scene->mNumMeshes= static_cast(_meshes.size()); - scene->mMeshes=to_array( _meshes ); + scene->mMeshes = unique_to_array( _meshes ); //animations if( _animations.size()==1 && _nodeAnims.size() ){ diff --git a/code/B3DImporter.h b/code/B3DImporter.h index b283cfab6..ef36c4618 100644 --- a/code/B3DImporter.h +++ b/code/B3DImporter.h @@ -123,7 +123,7 @@ private: std::vector _vertices; std::vector _nodes; - std::vector _meshes; + std::vector > _meshes; std::vector _nodeAnims; std::vector > _animations; }; From 08a35d4e1f44030276293d4a64863e802cee8464 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 19:16:06 +0200 Subject: [PATCH 4/6] B3DImporter: Store materials in unique_ptr --- code/B3DImporter.cpp | 8 ++++---- code/B3DImporter.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index d764bd530..195ee04a8 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -309,8 +309,7 @@ void B3DImporter::ReadBRUS(){ /*int blend=**/ReadInt(); int fx=ReadInt(); - aiMaterial *mat=new aiMaterial; - _materials.push_back( mat ); + std::unique_ptr mat(new aiMaterial); // Name aiString ainame( name ); @@ -347,6 +346,7 @@ void B3DImporter::ReadBRUS(){ mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) ); } } + _materials.emplace_back( std::move(mat) ); } } @@ -702,10 +702,10 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ //material if( !_materials.size() ){ - _materials.push_back( new aiMaterial ); + _materials.emplace_back( std::unique_ptr(new aiMaterial) ); } scene->mNumMaterials= static_cast(_materials.size()); - scene->mMaterials=to_array( _materials ); + scene->mMaterials = unique_to_array( _materials ); //meshes scene->mNumMeshes= static_cast(_meshes.size()); diff --git a/code/B3DImporter.h b/code/B3DImporter.h index ef36c4618..f9645f594 100644 --- a/code/B3DImporter.h +++ b/code/B3DImporter.h @@ -117,7 +117,7 @@ private: std::vector _stack; std::vector _textures; - std::vector _materials; + std::vector > _materials; int _vflags,_tcsets,_tcsize; std::vector _vertices; From 824dfc314b80dc0e91505cb08a65a50e4e8d2ded Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 19:39:41 +0200 Subject: [PATCH 5/6] B3DImporter: Store node animations in unique_ptr --- code/B3DImporter.cpp | 13 ++++++++----- code/B3DImporter.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index 195ee04a8..dd7b0d95e 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -546,7 +546,7 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){ node->mParent=parent; node->mTransformation=tform; - aiNodeAnim *nodeAnim=0; + std::unique_ptr nodeAnim; vector meshes; vector children; @@ -564,11 +564,10 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){ ReadANIM(); }else if( t=="KEYS" ){ if( !nodeAnim ){ - nodeAnim=new aiNodeAnim; - _nodeAnims.push_back( nodeAnim ); + nodeAnim.reset(new aiNodeAnim); nodeAnim->mNodeName=node->mName; } - ReadKEYS( nodeAnim ); + ReadKEYS( nodeAnim.get() ); }else if( t=="NODE" ){ aiNode *child=ReadNODE( node ); children.push_back( child ); @@ -576,6 +575,10 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){ ExitChunk(); } + if (nodeAnim) { + _nodeAnims.emplace_back( std::move(nodeAnim) ); + } + node->mNumMeshes= static_cast(meshes.size()); node->mMeshes=to_array( meshes ); @@ -716,7 +719,7 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ aiAnimation *anim = _animations.back().get(); anim->mNumChannels=static_cast(_nodeAnims.size()); - anim->mChannels=to_array( _nodeAnims ); + anim->mChannels = unique_to_array( _nodeAnims ); scene->mNumAnimations=static_cast(_animations.size()); scene->mAnimations=unique_to_array( _animations ); diff --git a/code/B3DImporter.h b/code/B3DImporter.h index f9645f594..342b88a28 100644 --- a/code/B3DImporter.h +++ b/code/B3DImporter.h @@ -124,7 +124,7 @@ private: std::vector _nodes; std::vector > _meshes; - std::vector _nodeAnims; + std::vector > _nodeAnims; std::vector > _animations; }; From 89afe0780b99e7309cf6158ce3e23e1e76f242f3 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Tue, 12 Dec 2017 20:03:16 +0200 Subject: [PATCH 6/6] B3DImporter: Fix double free when reusing Importer --- code/B3DImporter.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index dd7b0d95e..aa87609d4 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -702,6 +702,7 @@ void B3DImporter::ReadBB3D( aiScene *scene ){ //nodes scene->mRootNode=_nodes[0]; + _nodes.clear(); // node ownership now belongs to scene //material if( !_materials.size() ){