From 02d39080c8e3f66d7ff77d5cc9617d8ba4174767 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Wed, 18 Jul 2012 00:17:39 -0700
Subject: [PATCH] Destroy entities when they're done with.

---
 apps/openmw/mwrender/animation.cpp    |  5 +++++
 apps/openmw/mwrender/npcanimation.cpp | 20 ++++++++++++++++++++
 apps/openmw/mwrender/npcanimation.hpp |  1 -
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp
index 17c3f62ac..fddfe7b8a 100644
--- a/apps/openmw/mwrender/animation.cpp
+++ b/apps/openmw/mwrender/animation.cpp
@@ -5,6 +5,7 @@
 #include <OgreEntity.h>
 #include <OgreBone.h>
 #include <OgreSubMesh.h>
+#include <OgreSceneManager.h>
 
 namespace MWRender
 {
@@ -28,6 +29,10 @@ Animation::Animation(OEngine::Render::OgreRenderer& _rend)
 
 Animation::~Animation()
 {
+    Ogre::SceneManager *sceneMgr = mInsert->getCreator();
+    for(size_t i = 0;i < mEntityList.mEntities.size();i++)
+        sceneMgr->destroyEntity(mEntityList.mEntities[i]);
+    mEntityList.mEntities.clear();
 }
 
 void Animation::startScript(std::string groupname, int mode, int loops)
diff --git a/apps/openmw/mwrender/npcanimation.cpp b/apps/openmw/mwrender/npcanimation.cpp
index d740dd7db..51bad3d0e 100644
--- a/apps/openmw/mwrender/npcanimation.cpp
+++ b/apps/openmw/mwrender/npcanimation.cpp
@@ -17,6 +17,26 @@ using namespace NifOgre;
 namespace MWRender{
 NpcAnimation::~NpcAnimation()
 {
+    removeEntities(head);
+    removeEntities(hair);
+    removeEntities(neck);
+    removeEntities(groin);
+    removeEntities(rWrist);
+    removeEntities(lWrist);
+    removeEntities(rForearm);
+    removeEntities(lForearm);
+    removeEntities(rupperArm);
+    removeEntities(lupperArm);
+    removeEntities(rfoot);
+    removeEntities(lfoot);
+    removeEntities(rAnkle);
+    removeEntities(lAnkle);
+    removeEntities(rKnee);
+    removeEntities(lKnee);
+    removeEntities(rUpperLeg);
+    removeEntities(lUpperLeg);
+    removeEntities(rclavicle);
+    removeEntities(lclavicle);
 }
 
 
diff --git a/apps/openmw/mwrender/npcanimation.hpp b/apps/openmw/mwrender/npcanimation.hpp
index f27642720..6fb549408 100644
--- a/apps/openmw/mwrender/npcanimation.hpp
+++ b/apps/openmw/mwrender/npcanimation.hpp
@@ -40,7 +40,6 @@ private:
     std::vector<Ogre::Entity*> hair;
     std::vector<Ogre::Entity*> head;
 
-    Ogre::SceneNode* mInsert;
     bool isBeast;
     bool isFemale;
     std::string headModel;