Ensure pointers aren't used anymore when their parents are being deleted and they should be deleted themselves.

This commit is contained in:
Deukhoofd 2020-05-20 14:47:17 +02:00
parent 9bcfdf7791
commit eb99fc5661
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 72 additions and 3 deletions

View File

@ -143,6 +143,12 @@ namespace PkmnLibSharp.Library
{ {
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
_moves?.MarkAsDeleted();
}
protected override void DeletePtr() protected override void DeletePtr()
{ {
SpeciesVariant.Destruct(Ptr); SpeciesVariant.Destruct(Ptr);

View File

@ -63,6 +63,15 @@ namespace PkmnLibSharp.Library.Items
{ {
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var item in _cache)
{
item.Value.MarkAsDeleted();
}
}
protected override void DeletePtr() protected override void DeletePtr()
{ {
Creatureliblibrary.Generated.ItemLibrary.Destruct(Ptr); Creatureliblibrary.Generated.ItemLibrary.Destruct(Ptr);

View File

@ -66,6 +66,15 @@ namespace PkmnLibSharp.Library.Moves
{ {
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var moveData in _cache)
{
moveData.Value.MarkAsDeleted();
}
}
protected override void DeletePtr() protected override void DeletePtr()
{ {
AttackLibrary.Destruct(Ptr); AttackLibrary.Destruct(Ptr);

View File

@ -58,5 +58,14 @@ namespace PkmnLibSharp.Library
{ {
Pkmnlib.Generated.NatureLibrary.Destruct(Ptr); Pkmnlib.Generated.NatureLibrary.Destruct(Ptr);
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var nature in _cache)
{
nature.Value.MarkAsDeleted();
}
}
} }
} }

View File

@ -133,5 +133,17 @@ namespace PkmnLibSharp.Library
{ {
Pkmnlib.Generated.PokemonLibrary.Destruct(Ptr); Pkmnlib.Generated.PokemonLibrary.Destruct(Ptr);
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
_settings?.MarkAsDeleted();
_species?.MarkAsDeleted();
_moves?.MarkAsDeleted();
_items?.MarkAsDeleted();
_growthRateLibrary?.MarkAsDeleted();
_typeLibrary?.MarkAsDeleted();
_natureLibrary?.MarkAsDeleted();
}
} }
} }

View File

@ -84,6 +84,15 @@ namespace PkmnLibSharp.Library
return s; return s;
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var forme in _formes)
{
forme.Value.MarkAsDeleted();
}
}
protected override void DeletePtr() protected override void DeletePtr()
{ {
PokemonSpecies.Destruct(Ptr); PokemonSpecies.Destruct(Ptr);

View File

@ -67,5 +67,14 @@ namespace PkmnLibSharp.Library
{ {
Creatureliblibrary.Generated.SpeciesLibrary.Destruct(Ptr); Creatureliblibrary.Generated.SpeciesLibrary.Destruct(Ptr);
} }
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var species in _cache)
{
species.Value.MarkAsDeleted();
}
}
} }
} }

View File

@ -22,4 +22,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" /> <PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Battling" />
</ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
namespace PkmnLibSharp.Utilities namespace PkmnLibSharp.Utilities
{ {
@ -70,9 +68,14 @@ namespace PkmnLibSharp.Utilities
{ {
if (_isDeleted) if (_isDeleted)
return; return;
Cached.TryRemove(Ptr, out _);
DeletePtr(); DeletePtr();
MarkAsDeleted();
}
protected internal virtual void MarkAsDeleted()
{
_isDeleted = true; _isDeleted = true;
Cached.TryRemove(_ptr, out _);
} }
protected bool Equals(PointerWrapper other) protected bool Equals(PointerWrapper other)