OSDN Git Service

fix memory leak bug in jmePhysicsSpace.
authorKazuhiko Kobayashi <chototsu_moushinp@yahoo.co.jp>
Tue, 12 Jun 2012 22:36:36 +0000 (07:36 +0900)
committerkobayasi <kobayasi@pscnet.co.jp>
Sat, 6 Jul 2013 01:36:59 +0000 (10:36 +0900)
engine/src/bullet/com/jme3/bullet/PhysicsSpace.java
engine/src/bullet/native/jmePhysicsSpace.cpp
engine/src/bullet/native/jmePhysicsSpace.h

index 86ecf18..a6cf633 100644 (file)
@@ -151,7 +151,7 @@ public class PhysicsSpace {
         //TODO: boroadphase!
         physicsSpaceId = createPhysicsSpace(worldMin.x, worldMin.y, worldMin.z, worldMax.x, worldMax.y, worldMax.z, 3, false);
         pQueueTL.set(pQueue);
-        physicsSpaceTL.set(this);
+//        physicsSpaceTL.set(this);
 
 //        collisionConfiguration = new DefaultCollisionConfiguration();
 //        dispatcher = new CollisionDispatcher(collisionConfiguration);
@@ -437,8 +437,10 @@ public class PhysicsSpace {
             removeCollisionObject((PhysicsCollisionObject) obj);
         } else if (obj instanceof PhysicsJoint) {
             removeJoint((PhysicsJoint) obj);
+        } else if (obj == null) {
+            return;
         } else {
-            throw (new UnsupportedOperationException("Cannot remove this kind of object from the physics space."));
+            throw (new UnsupportedOperationException("Cannot remove this kind of object from the physics space."+obj));
         }
     }
 
@@ -920,17 +922,25 @@ public class PhysicsSpace {
 
     @Override
     protected void finalize() throws Throwable {
-        for(PhysicsJoint o : physicsJoints) {
-            remove(o);
-        }
-        physicsJoints.clear();
-        for(PhysicsCollisionObject o : physicsNodes.values()) {
-            remove(o);
+        for(;;) {
+            try {
+                while(physicsJoints.size() > 0) {
+                    remove(physicsJoints.get(0));
+                }
+                while(physicsNodes.size() > 0) {
+                    for(PhysicsRigidBody node : physicsNodes.values()) {
+                        remove(node);
+                        break;
+                    }
+                }
+                super.finalize();
+                Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Finalizing PhysicsSpace {0}", Long.toHexString(physicsSpaceId));
+                finalizeNative(physicsSpaceId);
+                break;
+            } catch(Exception ex) {
+                Logger.getLogger(PhysicsSpace.class.getName()).log(Level.SEVERE, "finalize failed.", ex);
+            }
         }
-        physicsNodes.clear();
-        super.finalize();
-        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Finalizing PhysicsSpace {0}", Long.toHexString(physicsSpaceId));
-        finalizeNative(physicsSpaceId);
     }
 
     private native void finalizeNative(long objectId);
index 9ae3021..13c5bbc 100644 (file)
@@ -106,12 +106,12 @@ void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ,
     //    if(threading){
     //        cci.m_defaultMaxPersistentManifoldPoolSize = 32768;
     //    }
-    btCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(cci);
+    collisionConfiguration = new btDefaultCollisionConfiguration(cci);
 
     btVector3 min = btVector3(minX, minY, minZ);
     btVector3 max = btVector3(maxX, maxY, maxZ);
 
-    btBroadphaseInterface* broadphase;
+    // btBroadphaseInterface* broadphase;
 
     switch (broadphaseId) {
         case 0:
@@ -135,8 +135,8 @@ void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ,
             break;
     }
 
-    btCollisionDispatcher* dispatcher;
-    btConstraintSolver* solver;
+    // btCollisionDispatcher* dispatcher;
+    // btConstraintSolver* solver;
     // use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
     if (threading) {
         btThreadSupportInterface* dispatchThreads = createDispatchThreadSupport(4);
@@ -166,8 +166,8 @@ void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ,
         world->getSolverInfo().m_solverMode = SOLVER_SIMD + SOLVER_USE_WARMSTARTING; //+SOLVER_RANDMIZE_ORDER;
         world->getDispatchInfo().m_enableSPU = true;
     }
-
-    broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
+    ghostPairCallback = new btGhostPairCallback();
+    broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(ghostPairCallback);
 
     dynamicsWorld->setGravity(btVector3(0, -9.81f, 0));
 
@@ -267,6 +267,11 @@ jobject jmePhysicsSpace::getJavaPhysicsSpace() {
 }
 
 jmePhysicsSpace::~jmePhysicsSpace() {
+    delete dispatcher;
+    delete solver;
+    delete broadphase;
+    delete collisionConfiguration;
+    delete ghostPairCallback;
     delete(dynamicsWorld);
     getEnv()->DeleteWeakGlobalRef(this->javaPhysicsSpace);
 }
index 24a6d70..9dcc206 100644 (file)
@@ -57,6 +57,13 @@ private:
        JNIEnv* env;
        JavaVM* vm;
        btDynamicsWorld* dynamicsWorld;
+
+        btCollisionDispatcher* dispatcher;
+        btConstraintSolver* solver;
+        btBroadphaseInterface* broadphase;
+        btCollisionConfiguration* collisionConfiguration;
+        btOverlappingPairCallback *ghostPairCallback;
+
        jobject javaPhysicsSpace;
         btThreadSupportInterface* createSolverThreadSupport(int);
         btThreadSupportInterface* createDispatchThreadSupport(int);