Skip to content

Commit

Permalink
jobs: Properly dispose physics state.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Oct 28, 2023
1 parent b043a0c commit 347b1a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,14 @@ public ref T GetAsRef(int index)
}
return ref UnsafeUtility.ArrayElementAsRef<T>(_buffer, index);
}

public T[] ToArray()
{
var array = new T[Length];
for (var i = 0; i < Length; i++) {
array[i] = UnsafeUtility.ReadArrayElement<T>(_buffer, i);
}
return array;
}
}
}
22 changes: 22 additions & 0 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ private void OnDestroy()
_ballStates.Dispose();
_colliders.Dispose();
_insideOfs.Dispose();
_octree.Dispose();
_bumperStates.Dispose();
_dropTargetStates.Dispose();
_flipperStates.Dispose();
_gateStates.Dispose();
_hitTargetStates.Dispose();
using (var enumerator = _kickerStates.GetEnumerator()) {
while (enumerator.MoveNext()) {
enumerator.Current.Value.Dispose();
}
}
_kickerStates.Dispose();
_plungerStates.Dispose();
_spinnerStates.Dispose();
_surfaceStates.Dispose();
using (var enumerator = _triggerStates.GetEnumerator()) {
while (enumerator.MoveNext()) {
enumerator.Current.Value.Dispose();
}
}
_triggerStates.Dispose();
_disabledCollisionItems.Dispose();
}

#endregion
Expand Down
33 changes: 1 addition & 32 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System;
using NativeTrees;
using Unity.Collections;
using VisualPinball.Engine.VPT;
using VisualPinball.Unity.Collections;

namespace VisualPinball.Unity
{
internal struct PhysicsState : IDisposable
internal struct PhysicsState
{
internal PhysicsEnv Env;
internal NativeOctree<int> Octree;
Expand Down Expand Up @@ -184,35 +183,5 @@ private bool IsInactiveDropTarget(int colliderId)
}

#endregion

public void Dispose()
{
Env.Dispose();
Octree.Dispose();
Colliders.Dispose();
InsideOfs.Dispose();
Balls.Dispose();
BumperStates.Dispose();
DropTargetStates.Dispose();
FlipperStates.Dispose();
GateStates.Dispose();
HitTargetStates.Dispose();
using (var enumerator = KickerStates.GetEnumerator()) {
while (enumerator.MoveNext()) {
enumerator.Current.Value.Dispose();
}
}
KickerStates.Dispose();
PlungerStates.Dispose();
SpinnerStates.Dispose();
SurfaceStates.Dispose();
using (var enumerator = TriggerStates.GetEnumerator()) {
while (enumerator.MoveNext()) {
enumerator.Current.Value.Dispose();
}
}
TriggerStates.Dispose();
DisabledCollisionItems.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public FlipperCorrectionState(bool isEnabled, int flipperItemId, uint timeDelayM
IsEnabled = isEnabled;
FlipperItemId = flipperItemId;
TimeDelayMs = timeDelayMs;
_polarities = Allocate(polarities, allocator);
_velocities = Allocate(velocities, allocator);
_allocator = allocator;
_polarities = Allocate(polarities, _allocator);
_velocities = Allocate(velocities, _allocator);
_numPolarities = polarities.Length;
_numVelocities = velocities.Length;
}
Expand All @@ -63,8 +63,8 @@ public FlipperCorrectionState(bool isEnabled, int flipperItemId, uint timeDelayM

public void Dispose()
{
UnsafeUtility.Free(_velocities, _allocator);
UnsafeUtility.Free(_polarities, _allocator);
UnsafeUtility.Free(_velocities, Allocator.None);
UnsafeUtility.Free(_polarities, Allocator.Temp);

_polarities = null;
_velocities = null;
Expand Down

0 comments on commit 347b1a0

Please sign in to comment.