Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not manually free materials for shapes #215

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
67 changes: 67 additions & 0 deletions physx/tests/bug-180.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use physx::{base::RefCounted, prelude::*};

type PxMaterial = physx::material::PxMaterial<()>;
type PxShape = physx::shape::PxShape<(), PxMaterial>;
type PxArticulationLink = physx::articulation_link::PxArticulationLink<(), PxShape>;
type PxRigidStatic = physx::rigid_static::PxRigidStatic<(), PxShape>;
type PxRigidDynamic = physx::rigid_dynamic::PxRigidDynamic<(), PxShape>;

/// Next up, the simulation event callbacks need to be defined, and possibly an
/// allocator callback as well.
struct OnCollision;
impl CollisionCallback for OnCollision {
fn on_collision(
&mut self,
_header: &physx_sys::PxContactPairHeader,
_pairs: &[physx_sys::PxContactPair],
) {
}
}
struct OnTrigger;
impl TriggerCallback for OnTrigger {
fn on_trigger(&mut self, _pairs: &[physx_sys::PxTriggerPair]) {}
}

struct OnConstraintBreak;
impl ConstraintBreakCallback for OnConstraintBreak {
fn on_constraint_break(&mut self, _constraints: &[physx_sys::PxConstraintInfo]) {}
}
struct OnWakeSleep;
impl WakeSleepCallback<PxArticulationLink, PxRigidStatic, PxRigidDynamic> for OnWakeSleep {
fn on_wake_sleep(
&mut self,
_actors: &[&physx::actor::ActorMap<PxArticulationLink, PxRigidStatic, PxRigidDynamic>],
_is_waking: bool,
) {
}
}

struct OnAdvance;
impl AdvanceCallback<PxArticulationLink, PxRigidDynamic> for OnAdvance {
fn on_advance(
&self,
_actors: &[&physx::rigid_body::RigidBodyMap<PxArticulationLink, PxRigidDynamic>],
_transforms: &[PxTransform],
) {
}
}

#[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);
}