Browse Source

bullet: Fix memory leak getting persistent manifolds from Python

These classes ought to be returned by value. Cleaner fix will be in master.

Fixes #1193
rdb 4 years ago
parent
commit
ad7cbcd4ec

+ 17 - 0
panda/src/bullet/bulletPersistentManifold.cxx

@@ -49,6 +49,8 @@ get_contact_processing_threshold() const {
  */
 void BulletPersistentManifold::
 clear_manifold() {
+  nassertv_always(_manifold != nullptr);
+
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
   _manifold->clearManifold();
@@ -59,6 +61,8 @@ clear_manifold() {
  */
 PandaNode *BulletPersistentManifold::
 get_node0() {
+  nassertr_always(_manifold != nullptr, nullptr);
+
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
 #if BT_BULLET_VERSION >= 281
@@ -75,6 +79,8 @@ get_node0() {
  */
 PandaNode *BulletPersistentManifold::
 get_node1() {
+  nassertr_always(_manifold != nullptr, nullptr);
+
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
 #if BT_BULLET_VERSION >= 281
@@ -91,6 +97,8 @@ get_node1() {
  */
 int BulletPersistentManifold::
 get_num_manifold_points() const {
+  nassertr_always(_manifold != nullptr, 0);
+
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
   return _manifold->getNumContacts();
@@ -107,3 +115,12 @@ get_manifold_point(int idx) const {
 
   return new BulletManifoldPoint(_manifold->getContactPoint(idx));
 }
+
+/**
+ *
+ */
+BulletManifoldPoint BulletPersistentManifold::
+__get_manifold_point(int idx) const {
+  LightMutexHolder holder(BulletWorld::get_global_lock());
+  return BulletManifoldPoint(_manifold->getContactPoint(idx));
+}

+ 3 - 2
panda/src/bullet/bulletPersistentManifold.h

@@ -35,7 +35,8 @@ PUBLISHED:
 
   int get_num_manifold_points() const;
   BulletManifoldPoint *get_manifold_point(int idx) const;
-  MAKE_SEQ(get_manifold_points, get_num_manifold_points, get_manifold_point);
+  BulletManifoldPoint __get_manifold_point(int idx) const;
+  MAKE_SEQ(get_manifold_points, get_num_manifold_points, __get_manifold_point);
 
   PN_stdfloat get_contact_breaking_threshold() const;
   PN_stdfloat get_contact_processing_threshold() const;
@@ -44,7 +45,7 @@ PUBLISHED:
 
   MAKE_PROPERTY(node0, get_node0);
   MAKE_PROPERTY(node1, get_node1);
-  MAKE_SEQ_PROPERTY(manifold_points, get_num_manifold_points, get_manifold_point);
+  MAKE_SEQ_PROPERTY(manifold_points, get_num_manifold_points, __get_manifold_point);
   MAKE_PROPERTY(contact_breaking_threshold, get_contact_breaking_threshold);
   MAKE_PROPERTY(contact_processing_threshold, get_contact_processing_threshold);
 

+ 13 - 0
panda/src/bullet/bulletWorld.cxx

@@ -1070,6 +1070,19 @@ get_manifold(int idx) const {
   return (ptr) ? new BulletPersistentManifold(ptr) : nullptr;
 }
 
+/**
+ *
+ */
+BulletPersistentManifold BulletWorld::
+__get_manifold(int idx) const {
+  LightMutexHolder holder(get_global_lock());
+
+  nassertr(idx < _dispatcher->getNumManifolds(), BulletPersistentManifold(nullptr));
+
+  btPersistentManifold *ptr = _dispatcher->getManifoldByIndexInternal(idx);
+  return BulletPersistentManifold(ptr);
+}
+
 /**
  *
  */

+ 3 - 2
panda/src/bullet/bulletWorld.h

@@ -128,7 +128,8 @@ PUBLISHED:
   // Manifolds
   int get_num_manifolds() const;
   BulletPersistentManifold *get_manifold(int idx) const;
-  MAKE_SEQ(get_manifolds, get_num_manifolds, get_manifold);
+  BulletPersistentManifold __get_manifold(int idx) const;
+  MAKE_SEQ(get_manifolds, get_num_manifolds, __get_manifold);
 
   // Collision filtering
   void set_group_collision_flag(unsigned int group1, unsigned int group2, bool enable);
@@ -168,7 +169,7 @@ PUBLISHED:
   MAKE_SEQ_PROPERTY(characters, get_num_characters, get_character);
   MAKE_SEQ_PROPERTY(vehicles, get_num_vehicles, get_vehicle);
   MAKE_SEQ_PROPERTY(constraints, get_num_constraints, get_constraint);
-  MAKE_SEQ_PROPERTY(manifolds, get_num_manifolds, get_manifold);
+  MAKE_SEQ_PROPERTY(manifolds, get_num_manifolds, __get_manifold);
   MAKE_PROPERTY(force_update_all_aabbs, get_force_update_all_aabbs,
                                         set_force_update_all_aabbs);