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

Fix many memory leaks #486

Merged
merged 2 commits into from
Nov 8, 2024
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
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