Skip to content

Commit

Permalink
Do not manually free materials for shapes (#215)
Browse files Browse the repository at this point in the history
* Do not manually free materials for shapes

* remove unnecessary junk
  • Loading branch information
tgolsson authored Oct 5, 2023
1 parent 11db087 commit 1ef3b01
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
resolver = "2"
members = ["physx", "physx-sys", "physx-sys/pxbind"]
2 changes: 1 addition & 1 deletion physx/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub trait Material: Class<physx_sys::PxMaterial> + UserData {
}

/// Set the restitution.
/// - Restitution must be in [0.0 ..= 1.0], values outside tyhe range are clamped.
/// - Restitution must be in [0.0 ..= 1.0], values outside the range are clamped.
/// - A reitution of 0.0 minimizes bouncing, higher values mean more bounce.
#[inline]
fn set_restitution(&mut self, mut restitution: f32) {
Expand Down
3 changes: 0 additions & 3 deletions physx/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ unsafe impl<U, M: Material> UserData for PxShape<U, M> {
impl<U, M: Material> Drop for PxShape<U, M> {
fn drop(&mut self) {
unsafe {
for material in self.get_materials_mut() {
drop_in_place(material as *mut _);
}
drop_in_place(self.get_user_data_mut());
use crate::base::RefCounted;
self.release();
Expand Down
24 changes: 24 additions & 0 deletions physx/tests/bug-180.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use physx::{base::RefCounted, prelude::*};

type PxMaterial = physx::material::PxMaterial<()>;
type PxShape = physx::shape::PxShape<(), PxMaterial>;

#[test]
fn test_double_free() {
let mut physics = PhysicsFoundation::<_, PxShape>::default();

let mut material = physics.create_material(0.5, 0.5, 0.6, ()).unwrap();
let geometry = PxBoxGeometry::new(1., 1., 1.);
let flags =
ShapeFlags::SceneQueryShape | ShapeFlags::SimulationShape | ShapeFlags::Visualization;

let shape = physics
.create_shape(&geometry, &mut [&mut material], false, flags, ())
.unwrap();

assert_eq!(material.get_reference_count(), 2);
drop(shape);

assert_eq!(material.get_reference_count(), 1);
drop(material);
}

0 comments on commit 1ef3b01

Please sign in to comment.