From d5bbc18b1ac633d4a6bbce1bfae3dae2df97247a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 16 Jan 2021 14:48:07 +0100 Subject: [PATCH] Fixes for scopedowner. --- PkmnLibSharp/Utilities/ScopedOwner.cs | 14 ++++++-------- .../Battling/BattleTests/BasicBattleTests.cs | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/PkmnLibSharp/Utilities/ScopedOwner.cs b/PkmnLibSharp/Utilities/ScopedOwner.cs index bb3f640..0ac000c 100644 --- a/PkmnLibSharp/Utilities/ScopedOwner.cs +++ b/PkmnLibSharp/Utilities/ScopedOwner.cs @@ -10,6 +10,7 @@ namespace PkmnLibSharp.Utilities public class ScopedOwner : IDisposable where T : PointerWrapper { public T? Value { get; private set; } + private bool _isOwner = true; public ScopedOwner(T value) { @@ -18,19 +19,16 @@ namespace PkmnLibSharp.Utilities public T? TakeOwnership() { - var val = Value; - Value = null; - return val; + _isOwner = false; + return Value; } ~ScopedOwner() { - if (Value != null) + if (_isOwner && Value != null) Value.Dispose(); } - - public static implicit operator T?(ScopedOwner val) => val.Value; - + public override string ToString() { if (Value == null) return "null"; @@ -39,7 +37,7 @@ namespace PkmnLibSharp.Utilities public void Dispose() { - if (Value != null) + if (_isOwner && Value != null) Value.Dispose(); Value = null; } diff --git a/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs b/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs index 5745573..272e3d5 100644 --- a/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs +++ b/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs @@ -30,7 +30,7 @@ namespace PkmnLibSharpTests.Battling.BattleTests party.SwapInto(0, new PokemonBuilder(lib, "testSpecies", 50) .LearnMove("testMove", MoveLearnMethod.Unknown) - .Build()); + .Build().TakeOwnership()); return party; }