Skip to content

Commit

Permalink
Fix many memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkehrwald authored and freezy committed Nov 8, 2024
1 parent 91887e3 commit 0432a12
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 171 deletions.
28 changes: 28 additions & 0 deletions VisualPinball.Unity/VisualPinball.Unity/Common/LazyInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace VisualPinball.Unity
{
public class LazyInit<T>
{
private bool isInitialized = false;
private T value;
public ref T Ref
{
get
{
if (!isInitialized) {
isInitialized = true;
value = constructor();
}
return ref value;
}
}

private readonly Func<T> constructor;

public LazyInit(Func<T> constructor)
{
this.constructor = constructor;
}
}
}
11 changes: 11 additions & 0 deletions VisualPinball.Unity/VisualPinball.Unity/Common/LazyInit.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsCycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ internal void Simulate(ref PhysicsState state, in AABB playfieldBounds, ref Nati

// create octree of ball-to-ball collision
// it's okay to have this code outside of the inner loop, as the ball hitrects already include the maximum distance they can travel in that timespan
var ballOctree = PhysicsDynamicBroadPhase.CreateOctree(ref state.Balls, in playfieldBounds);
using var ballOctree = PhysicsDynamicBroadPhase.CreateOctree(ref state.Balls, in playfieldBounds);

// create octree of kinematic-to-ball collision
PhysicsKinematicBroadPhase.TransformColliders(ref state);
var kineticOctree = PhysicsKinematicBroadPhase.CreateOctree(ref state.KinematicColliders, in playfieldBounds);
using var kineticOctree = PhysicsKinematicBroadPhase.CreateOctree(ref state.KinematicColliders, in playfieldBounds);

while (dTime > 0) {

Expand Down
Loading

0 comments on commit 0432a12

Please sign in to comment.