Skip to content

Commit

Permalink
clang fixes and fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Jan 27, 2025
1 parent eb4c15e commit ef2847e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down

0 comments on commit ef2847e

Please sign in to comment.