From bb20c96da562a294f3f606ca6db446389d6765cb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 11 Sep 2015 19:12:25 +1000 Subject: Fix memory leaks in collada import --- source/blender/collada/AnimationImporter.cpp | 6 +++-- source/blender/collada/DocumentImporter.cpp | 36 ++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/source/blender/collada/AnimationImporter.cpp b/source/blender/collada/AnimationImporter.cpp index dbff917d2c1..1bcf51f9d1f 100644 --- a/source/blender/collada/AnimationImporter.cpp +++ b/source/blender/collada/AnimationImporter.cpp @@ -898,8 +898,6 @@ void AnimationImporter::translate_Animations(COLLADAFW::Node *node, std::multimap& object_map, std::map FW_object_map) { - AnimationImporter::AnimMix *animType = get_animation_type(node, FW_object_map); - bool is_joint = node->getType() == COLLADAFW::Node::JOINT; COLLADAFW::UniqueId uid = node->getUniqueId(); COLLADAFW::Node *root = root_map.find(uid) == root_map.end() ? node : root_map[uid]; @@ -915,6 +913,8 @@ void AnimationImporter::translate_Animations(COLLADAFW::Node *node, return; } + + AnimationImporter::AnimMix *animType = get_animation_type(node, FW_object_map); bAction *act; if ( (animType->transform) != 0) { @@ -1116,6 +1116,8 @@ void AnimationImporter::translate_Animations(COLLADAFW::Node *node, } } } + + delete animType; } void AnimationImporter::add_bone_animation_sampled(Object *ob, std::vector& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node, COLLADAFW::Transformation *tm) diff --git a/source/blender/collada/DocumentImporter.cpp b/source/blender/collada/DocumentImporter.cpp index 0aff5147060..674a79f0fe7 100644 --- a/source/blender/collada/DocumentImporter.cpp +++ b/source/blender/collada/DocumentImporter.cpp @@ -136,11 +136,14 @@ bool DocumentImporter::import() const std::string encodedFilename = bc_url_encode(mFilename); if (!root.loadDocument(encodedFilename)) { fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 1st pass\n"); + delete ehandler; return false; } - if (errorHandler.hasError()) + if (errorHandler.hasError()) { + delete ehandler; return false; + } /** TODO set up scene graph and such here */ @@ -151,10 +154,10 @@ bool DocumentImporter::import() if (!root2.loadDocument(encodedFilename)) { fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 2nd pass\n"); + delete ehandler; return false; } - - + delete ehandler; return true; @@ -223,6 +226,7 @@ void DocumentImporter::finish() for (unsigned int i = 0; i < roots.getCount(); i++) { std::vector *objects_done = write_node(roots[i], NULL, sce, NULL, false); objects_to_scale->insert(objects_to_scale->end(), objects_done->begin(), objects_done->end()); + delete objects_done; } // update scene @@ -275,6 +279,8 @@ void DocumentImporter::finish() } bc_match_scale(objects_to_scale, unit_converter, !this->import_settings->import_units); + + delete objects_to_scale; } @@ -499,6 +505,9 @@ std::vector *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA std::string id = node->getOriginalId(); std::string name = node->getName(); + // if node has child nodes write them + COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes(); + std::vector *objects_done = new std::vector(); std::vector *root_objects = new std::vector(); @@ -524,7 +533,7 @@ std::vector *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA if (parent_node == NULL) { // for skeletons without root node all has been done above. // Skeletons with root node are handled further down. - return root_objects; + goto finally; } } else { @@ -638,7 +647,9 @@ std::vector *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA // XXX: if there're multiple instances, only one is stored - if (!ob) return root_objects; + if (!ob) { + goto finally; + } for (std::vector::iterator it = objects_done->begin(); it != objects_done->end(); ++it) { ob = *it; @@ -673,9 +684,6 @@ std::vector *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA } } - // if node has child nodes write them - COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes(); - if (objects_done->size() > 0) { ob = *objects_done->begin(); } @@ -684,9 +692,15 @@ std::vector *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA } for (unsigned int i = 0; i < child_nodes.getCount(); i++) { - write_node(child_nodes[i], node, sce, ob, is_library_node); + std::vector *child_objects; + child_objects = write_node(child_nodes[i], node, sce, ob, is_library_node); + delete child_objects; } + +finally: + delete objects_done; + return root_objects; } @@ -725,7 +739,9 @@ bool DocumentImporter::writeLibraryNodes(const COLLADAFW::LibraryNodes *libraryN const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes(); for (unsigned int i = 0; i < nodes.getCount(); i++) { - write_node(nodes[i], NULL, sce, NULL, true); + std::vector *child_objects; + child_objects = write_node(nodes[i], NULL, sce, NULL, true); + delete child_objects; } return true; -- cgit v1.2.3