From ef2847ee693e6ff47a9d1889928d3be67cf1e588 Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Sun, 26 Jan 2025 17:24:08 -0800 Subject: [PATCH] clang fixes and fix memory leak --- src/core.h | 2 +- src/shape.c | 14 ++++++++++---- src/shape.h | 2 ++ src/world.c | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core.h b/src/core.h index 2b7bcc474..cb7a5b4d8 100644 --- a/src/core.h +++ b/src/core.h @@ -110,7 +110,7 @@ #define B2_ARRAY_COUNT( A ) (int)( sizeof( A ) / sizeof( A[0] ) ) // Used to prevent the compiler from warning about unused variables -#define B2_UNUSED( ... ) (void)( __VA_ARGS__ ) +#define B2_UNUSED( ... ) (void)sizeof( ( __VA_ARGS__, 0 ) ) // Use to validate definitions. Do not take my cookie. #define B2_SECRET_COOKIE 1152023 diff --git a/src/shape.c b/src/shape.c index 302fd4300..a3bad3f8a 100644 --- a/src/shape.c +++ b/src/shape.c @@ -501,6 +501,15 @@ b2ChainId b2CreateChain( b2BodyId bodyId, const b2ChainDef* def ) return id; } +void b2FreeChainData(b2ChainShape* chain) +{ + b2Free( chain->shapeIndices, chain->count * sizeof( int ) ); + chain->shapeIndices = NULL; + + b2Free( chain->materials, chain->materialCount * sizeof( b2SurfaceMaterial ) ); + chain->materials = NULL; +} + void b2DestroyChain( b2ChainId chainId ) { b2World* world = b2GetWorldLocked( chainId.world0 ); @@ -543,10 +552,7 @@ void b2DestroyChain( b2ChainId chainId ) b2DestroyShapeInternal( world, shape, body, wakeBodies ); } - b2Free( chain->shapeIndices, chain->count * sizeof( int ) ); - chain->shapeIndices = NULL; - - b2Free( chain->materials, chain->materialCount * sizeof( b2SurfaceMaterial ) ); + b2FreeChainData( chain ); // Return chain to free list. b2FreeId( &world->chainIdPool, chain->id ); diff --git a/src/shape.h b/src/shape.h index 1afefb0f3..6d986f1dc 100644 --- a/src/shape.h +++ b/src/shape.h @@ -83,6 +83,8 @@ typedef struct void b2CreateShapeProxy( b2Shape* shape, b2BroadPhase* bp, b2BodyType type, b2Transform transform, bool forcePairCreation ); void b2DestroyShapeProxy( b2Shape* shape, b2BroadPhase* bp ); +void b2FreeChainData( b2ChainShape* chain ); + b2MassData b2ComputeShapeMass( const b2Shape* shape ); b2ShapeExtent b2ComputeShapeExtent( const b2Shape* shape, b2Vec2 localCenter ); b2AABB b2ComputeShapeAABB( const b2Shape* shape, b2Transform transform ); diff --git a/src/world.c b/src/world.c index fcc81bbb2..9bfd72801 100644 --- a/src/world.c +++ b/src/world.c @@ -296,11 +296,12 @@ void b2DestroyWorld( b2WorldId worldId ) b2ChainShape* chain = world->chainShapes.data + i; if ( chain->id != B2_NULL_INDEX ) { - b2Free( chain->shapeIndices, chain->count * sizeof( int ) ); + b2FreeChainData( chain ); } else { B2_ASSERT( chain->shapeIndices == NULL ); + B2_ASSERT( chain->materials == NULL ); } }