浏览代码

Fixes memory leak issue #407.

Ken Whatmough 13 年之前
父节点
当前提交
a217f29f09
共有 3 个文件被更改,包括 12 次插入4 次删除
  1. 6 2
      gameplay/src/PhysicsCollisionShape.cpp
  2. 4 1
      gameplay/src/PhysicsCollisionShape.h
  3. 2 1
      gameplay/src/PhysicsController.cpp

+ 6 - 2
gameplay/src/PhysicsCollisionShape.cpp

@@ -6,8 +6,8 @@
 namespace gameplay
 {
 
-PhysicsCollisionShape::PhysicsCollisionShape(Type type, btCollisionShape* shape)
-    : _type(type), _shape(shape)
+PhysicsCollisionShape::PhysicsCollisionShape(Type type, btCollisionShape* shape, btStridingMeshInterface* meshInterface)
+    : _type(type), _shape(shape), _meshInterface(meshInterface)
 {
     memset(&_shapeData, 0, sizeof(_shapeData));
 }
@@ -29,6 +29,10 @@ PhysicsCollisionShape::~PhysicsCollisionShape()
                 }
                 SAFE_DELETE(_shapeData.meshData);
             }
+
+            // Also need to delete the btTriangleIndexVertexArray, if it exists.
+            SAFE_DELETE(_meshInterface);
+
             break;
         case SHAPE_HEIGHTFIELD:
             if (_shapeData.heightfieldData)

+ 4 - 1
gameplay/src/PhysicsCollisionShape.h

@@ -227,7 +227,7 @@ private:
     /**
      * Constructor.
      */
-    PhysicsCollisionShape(Type type, btCollisionShape* shape);
+    PhysicsCollisionShape(Type type, btCollisionShape* shape, btStridingMeshInterface* meshInterface = NULL);
 
     /** 
      * Hidden copy constructor.
@@ -245,6 +245,9 @@ private:
     // Bullet shape object
     btCollisionShape* _shape;
 
+    // Bullet mesh interface for mesh types (NULL otherwise)
+    btStridingMeshInterface* _meshInterface;
+
     // Shape specific cached data
     union
     {

+ 2 - 1
gameplay/src/PhysicsController.cpp

@@ -1273,7 +1273,8 @@ PhysicsCollisionShape* PhysicsController::createMesh(Mesh* mesh, const Vector3&
     }
 
     // Create our collision shape object and store shapeMeshData in it.
-    PhysicsCollisionShape* shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_MESH, bullet_new<btBvhTriangleMeshShape>(meshInterface, true));
+    PhysicsCollisionShape* shape =
+        new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_MESH, bullet_new<btBvhTriangleMeshShape>(meshInterface, true), meshInterface);
     shape->_shapeData.meshData = shapeMeshData;
 
     _shapes.push_back(shape);