8000 Fix the memory leak issue of the physics by luzhuang · Pull Request #2648 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix the memory leak issue of the physics #2648

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

Merged
merged 5 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export class Engine extends EventDispatcher {
static _pixelsPerUnit: number = 100;
/** @internal */
static _physicalObjectsMap: Record<number, ColliderShape> = {};
/** @internal */
static _nativePhysics: IPhysics;

/** Input manager of Engine. */
readonly inputManager: InputManager;
Expand Down Expand Up @@ -505,7 +507,6 @@ export class Engine extends EventDispatcher {
this._hardwareRenderer.destroy();

this.removeAllEventListeners();

this._waitingDestroy = false;
this._destroyed = true;
}
Expand Down Expand Up @@ -622,7 +623,12 @@ export class Engine extends EventDispatcher {
if (physics) {
initializePromises.push(
physics.initialize().then(() => {
PhysicsScene._nativePhysics = physics;
if (Engine._nativePhysics) {
console.warn(
"A physics engine has already been configured. All physics operations will now be handled by the newly specified physics engine."
);
}
Engine._nativePhysics = physics;
this._nativePhysicsManager = physics.createPhysicsManager();
this._physicsInitialized = true;
return this;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ export class Scene extends EngineObject {

const allCreatedScenes = sceneManager._allCreatedScenes;
allCreatedScenes.splice(allCreatedScenes.indexOf(this), 1);

this.physics._destroy();
Comment on lines +517 to +518
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Added explicit physics destruction to fix memory leak

This is the critical change that fixes the memory leak issue. The scene now explicitly calls physics._destroy() during scene destruction, which properly cleans up physics resources that were previously leaking.

}

private _computeLinearFogParams(fogStart: number, fogEnd: number): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ICharacterController } from "@galacean/engine-design";
import { Vector3 } from "@galacean/engine-math";
import { Engine } from "../Engine";
import { Entity } from "../Entity";
import { Collider } from "./Collider";
import { PhysicsScene } from "./PhysicsScene";
import { ControllerNonWalkableMode } from "./enums/ControllerNonWalkableMode";
import { ColliderShape } from "./shape";
import { deepClone, ignoreClone } from "../clone/CloneManager";
Expand Down Expand Up @@ -80,7 +80,7 @@ export class CharacterController extends Collider {
*/
constructor(entity: Entity) {
super(entity);
(<ICharacterController>this._nativeCollider) = PhysicsScene._nativePhysics.createCharacterController();
(<ICharacterController>this._nativeCollider) = Engine._nativePhysics.createCharacterController();

this._setUpDirection = this._setUpDirection.bind(this);
//@ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/DynamicCollider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IDynamicCollider } from "@galacean/engine-design";
import { Quaternion, Vector3 } from "@galacean/engine-math";
import { Engine } from "../Engine";
import { ignoreClone } from "../clone/CloneManager";
import { Entity } from "../Entity";
import { Collider } from "./Collider";
import { PhysicsScene } from "./PhysicsScene";

/**
* A dynamic collider can act with self-defined movement or physical force.
Expand Down Expand Up @@ -306,7 +306,7 @@ export class DynamicCollider extends Collider {
constructor(entity: Entity) {
super(entity);
const { transform } = this.entity;
this._nativeCollider = PhysicsScene._nativePhysics.createDynamicCollider(
this._nativeCollider = Engine._nativePhysics.createDynamicCollider(
transform.worldPosition,
transform.worldRotationQuaternion
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/PhysicsMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPhysicsMaterial } from "@galacean/engine-design";
import { PhysicsScene } from "./PhysicsScene";
import { Engine } from "../Engine";
import { PhysicsMaterialCombineMode } from "./enums/PhysicsMaterialCombineMode";

/**
Expand All @@ -17,7 +17,7 @@ export class PhysicsMaterial {
_nativeMaterial: IPhysicsMaterial;

constructor() {
this._nativeMaterial = PhysicsScene._nativePhysics.createPhysicsMaterial(
this._nativeMaterial = Engine._nativePhysics.createPhysicsMaterial(
this._staticFriction,
this._dynamicFriction,
this._bounciness,
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/physics/PhysicsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* A physics scene is a collection of colliders and constraints which can interact.
*/
export class PhysicsScene {
/** @internal */
static _nativePhysics: IPhysics;

private static _collision = new Collision();

private _scene: Scene;
Expand Down Expand Up @@ -219,7 +216,7 @@

const engine = scene.engine;
if (engine._physicsInitialized) {
this._nativePhysicsScene = PhysicsScene._nativePhysics.createPhysicsScene(
this._nativePhysicsScene = Engine._nativePhysics.createPhysicsScene(

Check warning on line 219 in packages/core/src/physics/PhysicsScene.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/physics/PhysicsScene.ts#L219

Added line #L219 was not covered by tests
engine._nativePhysicsManager,
this._onContactEnter,
this._onContactExit,
Expand Down Expand Up @@ -445,6 +442,13 @@
this._colliders.garbageCollection();
}

/**
* @internal
*/
_destroy() {
this._nativePhysicsScene?.destroy();
}

private _setGravity(): void {
this._nativePhysicsScene.setGravity(this._gravity);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/StaticCollider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Engine } from "../Engine";
import { Entity } from "../Entity";
import { Collider } from "./Collider";
import { PhysicsScene } from "./PhysicsScene";

/**
* A static collider component that will not move.
Expand All @@ -13,7 +13,7 @@ export class StaticCollider extends Collider {
constructor(entity: Entity) {
super(entity);
const { transform } = this.entity;
this._nativeCollider = PhysicsScene._nativePhysics.createStaticCollider(
this._nativeCollider = Engine._nativePhysics.createStaticCollider(
transform.worldPosition,
transform.worldRotationQuaternion
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/joint/FixedJoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Engine } from "../../Engine";
import { Collider } from "../Collider";
import { PhysicsScene } from "../PhysicsScene";
import { Joint } from "./Joint";

/*
Expand All @@ -9,6 +9,6 @@ export class FixedJoint extends Joint {
protected _createJoint(): void {
const colliderInfo = this._colliderInfo;
colliderInfo.collider = this.entity.getComponent(Collider);
this._nativeJoint = PhysicsScene._nativePhysics.createFixedJoint(colliderInfo.collider._nativeCollider);
this._nativeJoint = Engine._nativePhysics.createFixedJoint(colliderInfo.collider._nativeCollider);
}
}
4 changes: 2 additions & 2 deletions packages/core/src/physics/joint/HingeJoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IHingeJoint } from "@galacean/engine-design";
import { Vector3 } from "@galacean/engine-math";
import { Engine } from "../../Engine";
import { Collider } from "../Collider";
import { PhysicsScene } from "../PhysicsScene";
import { HingeJointFlag } from "../enums/HingeJointFlag";
import { Joint } from "./Joint";
import { JointLimits } from "./JointLimits";
Expand Down Expand Up @@ -160,7 +160,7 @@ export class HingeJoint extends Joint {
protected _createJoint(): void {
const colliderInfo = this._colliderInfo;
colliderInfo.collider = this.entity.getComponent(Collider);
this._nativeJoint = PhysicsScene._nativePhysics.createHingeJoint(colliderInfo.collider._nativeCollider);
this._nativeJoint = Engine._nativePhysics.createHingeJoint(colliderInfo.collider._nativeCollider);
}

protected override _syncNative(): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/joint/SpringJoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ISpringJoint } from "@galacean/engine-design";
import { Engine } from "../../Engine";
import { Collider } from "../Collider";
import { PhysicsScene } from "../PhysicsScene";
import { Joint } from "./Joint";

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ export class SpringJoint extends Joint {
protected _createJoint(): void {
const colliderInfo = this._colliderInfo;
colliderInfo.collider = this.entity.getComponent(Collider);
this._nativeJoint = PhysicsScene._nativePhysics.createSpringJoint(colliderInfo.collider._nativeCollider);
this._nativeJoint = Engine._nativePhysics.createSpringJoint(colliderInfo.collider._nativeCollider);
}

protected override _syncNative(): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/shape/BoxColliderShape.ts
E377
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ColliderShape } from "./ColliderShape";
import { IBoxColliderShape } from "@galacean/engine-design";
import { Vector3 } from "@galacean/engine-math";
import { PhysicsScene } from "../PhysicsScene";
import { Engine } from "../../Engine";
import { deepClone, ignoreClone } from "../../clone/CloneManager";

/**
Expand All @@ -26,7 +26,7 @@ export class BoxColliderShape extends ColliderShape {

constructor() {
super();
this._nativeShape = PhysicsScene._nativePhysics.createBoxColliderShape(
this._nativeShape = Engine._nativePhysics.createBoxColliderShape(
this._id,
this._size,
this._material._nativeMaterial
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/physics/shape/CapsuleColliderShape.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ColliderShape } from "./ColliderShape";
import { ICapsuleColliderShape } from "@galacean/engine-design";
import { PhysicsScene } from "../PhysicsScene";
import { Engine } from "../../Engine";
import { ColliderShapeUpAxis } from "../enums/ColliderShapeUpAxis";
import { ignoreClone } from "../../clone/CloneManager";

/**
* Physical collider shape for capsule.
Expand Down Expand Up @@ -56,7 +55,7 @@ export class CapsuleColliderShape extends ColliderShape {

constructor() {
super();
this._nativeShape = PhysicsScene._nativePhysics.createCapsuleColliderShape(
this._nativeShape = Engine._nativePhysics.createCapsuleColliderShape(
this._id,
this._radius,
this._height,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/shape/PlaneColliderShape.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector3 } from "@galacean/engine-math";
import { PhysicsScene } from "../PhysicsScene";
import { Engine } from "../../Engine";
import { ColliderShape } from "./ColliderShape";

/**
Expand All @@ -8,7 +8,7 @@ import { ColliderShape } from "./ColliderShape";
export class PlaneColliderShape extends ColliderShape {
constructor() {
super();
this._nativeShape = PhysicsScene._nativePhysics.createPlaneColliderShape(this._id, this._material._nativeMaterial);
this._nativeShape = Engine._nativePhysics.createPlaneColliderShape(this._id, this._material._nativeMaterial);
}

override getClosestPoint(point: Vector3, closestPoint: Vector3): number {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/physics/shape/SphereColliderShape.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ColliderShape } from "./ColliderShape";
import { ISphereColliderShape } from "@galacean/engine-design";
import { PhysicsScene } from "../PhysicsScene";
import { ignoreClone } from "../../clone/CloneManager";
import { Engine } from "../../Engine";

/**
* Physical collider shape for sphere.
Expand All @@ -25,7 +24,7 @@ export class SphereColliderShape extends ColliderShape {

constructor() {
super();
this._nativeShape = PhysicsScene._nativePhysics.createSphereColliderShape(
this._nativeShape = Engine._nativePhysics.createSphereColliderShape(
this._id,
this._radius,
this._material._nativeMaterial
Expand Down
5 changes: 5 additions & 0 deletions packages/design/src/physics/IPhysics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ export interface IPhysics {
* @param collider - Affector of joint
*/
createSpringJoint(collider: ICollider): ISpringJoint;

/**
* Destroy physics scene.
*/
destroy(): void;
}
5 changes: 5 additions & 0 deletions packages/design/src/physics/IPhysicsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ export interface IPhysicsScene {
onRaycast: (obj: number) => boolean,
outHitResult?: (shapeUniqueID: number, distance: number, point: Vector3, normal: Vector3) => void
): boolean;

/**
* Destroy the physics scene.
*/
destroy(): void;
}
5 changes: 5 additions & 0 deletions packages/physics-lite/src/LitePhysics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ export class LitePhysics implements IPhysics {
createSpringJoint(collider: LiteCollider): ISpringJoint {
throw "Physics-lite don't support CapsuleColliderShape. Use Physics-PhysX instead!";
}

/**
* {@inheritDoc IPhysics.destroy }
*/
destroy(): void {}
}
19 changes: 12 additions & 7 deletions packages/physics-lite/src/LitePhysicsScene.ts
10000
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export class LitePhysicsScene implements IPhysicsScene {
}

/**
* {@inheritDoc IPhysicsManager.setGravity }
* {@inheritDoc IPhysicsScene.setGravity }
*/
setGravity(value: Vector3): void {
console.log("Physics-lite don't support gravity. Use Physics-PhysX instead!");
}

/**
* {@inheritDoc IPhysicsManager.addCollider }
* {@inheritDoc IPhysicsScene.addCollider }
*/
addCollider(actor: LiteCollider): void {
actor._scene = this;
Expand All @@ -70,7 +70,7 @@ export class LitePhysicsScene implements IPhysicsScene {
}

/**
* {@inheritDoc IPhysicsManager.removeCollider }
* {@inheritDoc IPhysicsScene.removeCollider }
*/
removeCollider(collider: LiteCollider): void {
collider._scene = null;
Expand All @@ -84,7 +84,7 @@ export class LitePhysicsScene implements IPhysicsScene {
}

/**
* {@inheritDoc IPhysicsManager.update }
* {@inheritDoc IPhysicsScene.update }
*/
update(deltaTime: number): void {
const dynamicColliders = this._dynamicColliders;
Expand All @@ -97,7 +97,7 @@ export class LitePhysicsScene implements IPhysicsScene {
}

/**
* {@inheritDoc IPhysicsManager.raycast }
* {@inheritDoc IPhysicsScene.raycast }
*/
raycast(
ray: Ray,
Expand Down Expand Up @@ -134,19 +134,24 @@ export class LitePhysicsScene implements IPhysicsScene {
}

/**
* {@inheritDoc IPhysicsManager.addCharacterController }
* {@inheritDoc IPhysicsScene.addCharacterController }
*/
addCharacterController(characterController: ICharacterController): void {
throw "Physics-lite don't support addCharacterController. Use Physics-PhysX instead!";
}

/**
* {@inheritDoc IPhysicsManager.removeCharacterController }
* {@inheritDoc IPhysicsScene.removeCharacterController }
*/
removeCharacterController(characterController: ICharacterController): void {
throw "Physics-lite don't support removeCharacterController. Use Physics-PhysX instead!";
}

/**
* {@inheritDoc IPhysicsScene.destroy }
*/
destroy(): void {}

/**
* @internal
*/
Expand Down
1 change: 1 addition & 0 deletions packages/physics-physx/src/PhysXCharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class PhysXCharacterController implements ICharacterController {

this._pxController = pxManager._getControllerManager().createController(desc);
this._pxController.setUUID(shape._id);
desc.delete();
}

/**
Expand Down
Loading
Loading
0