Skip to content

Commit

Permalink
Closed memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Jan 30, 2023
1 parent 62feeae commit 69acf87
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 226 deletions.
26 changes: 22 additions & 4 deletions src/FetchBannerlordVersion.Native/Bindings.Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace FetchBannerlordVersion.Native
{
public static unsafe partial class Bindings
{
[UnmanagedCallersOnly(EntryPoint = "alloc", CallConvs = new [] { typeof(CallConvCdecl) })]
[UnmanagedCallersOnly(EntryPoint = "alloc")]
public static void* Alloc(nuint size)
{
Logger.LogInput(size);
try
{
var result = NativeMemory.Alloc(size);
var result = Allocator.Alloc(size, true);

Logger.LogOutputPrimitive((int) result);
return result;
Expand All @@ -26,13 +26,13 @@ public static unsafe partial class Bindings
}
}

[UnmanagedCallersOnly(EntryPoint = "dealloc", CallConvs = new [] { typeof(CallConvCdecl) })]
[UnmanagedCallersOnly(EntryPoint = "dealloc")]
public static void Dealloc(param_ptr* ptr)
{
Logger.LogInput(ptr);
try
{
NativeMemory.Free(ptr);
Allocator.Free(ptr, true);

Logger.LogOutput();
}
Expand All @@ -41,5 +41,23 @@ public static void Dealloc(param_ptr* ptr)
Logger.LogException(e);
}
}

[UnmanagedCallersOnly(EntryPoint = "alloc_alive_count")]
public static int AllocAliveCount()
{
Logger.LogInput();
try
{
var result = Allocator.GetCurrentAllocations();

Logger.LogOutputPrimitive(result);
return result;
}
catch (Exception e)
{
Logger.LogException(e);
return -1;
}
}
}
}
12 changes: 6 additions & 6 deletions src/FetchBannerlordVersion.Native/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public static unsafe partial class Bindings
var result = (uint) Fetcher.GetChangeSet(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutputPrimitive(result);
return return_value_uint32.AsValue(result);
return return_value_uint32.AsValue(result, false);
}
catch (Exception e)
{
Logger.LogException(e);
return return_value_uint32.AsError(BUTR.NativeAOT.Shared.Utils.Copy(e.ToString()));
return return_value_uint32.AsError(Utils.Copy(e.ToString(), false), false);
}
}

Expand All @@ -42,12 +42,12 @@ public static unsafe partial class Bindings
var result = Fetcher.GetVersion(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutput(result);
return return_value_string.AsValue(BUTR.NativeAOT.Shared.Utils.Copy(result));
return return_value_string.AsValue(Utils.Copy(result, false), false);
}
catch (Exception e)
{
Logger.LogException(e);
return return_value_string.AsError(BUTR.NativeAOT.Shared.Utils.Copy(e.ToString()));
return return_value_string.AsError(Utils.Copy(e.ToString(), false), false);
}
}

Expand All @@ -63,12 +63,12 @@ public static unsafe partial class Bindings
var result = (uint) Fetcher.GetVersionType(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutputPrimitive(result);
return return_value_uint32.AsValue(result);
return return_value_uint32.AsValue(result, false);
}
catch (Exception e)
{
Logger.LogException(e);
return return_value_uint32.AsError(BUTR.NativeAOT.Shared.Utils.Copy(e.ToString()));
return return_value_uint32.AsError(Utils.Copy(e.ToString(), false), false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<IsPackable>false</IsPackable>

<DefineConstants>$(DefineConstants);TRACK_ALLOCATIONS;</DefineConstants>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -39,7 +42,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.24" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.36" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="ConcurrentHashSet" Version="1.3.0" />
</ItemGroup>

<Target Name="CopyHeaders" AfterTargets="AfterBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.25" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.36" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
Expand Down
15 changes: 6 additions & 9 deletions test/FetchBannerlordVersion.Native.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,20 @@ public unsafe void Test_Main()
{
Assert.DoesNotThrow(() =>
{
var path = Path.GetFullPath("./Data");
var dllName = "TaleWorlds.Library.dll";
using var path = Utils.Copy(Path.GetFullPath("./Data"), true);
using var dllName = Utils.Copy("TaleWorlds.Library.dll", true);

using var path2 = Copy(path);
using var dllName2 = Copy(dllName);

var changeSet = GetResult(bfv_get_change_set((param_string*) path2.DangerousGetHandle(), (param_string*) dllName2.DangerousGetHandle()));
var changeSet = GetResult(bfv_get_change_set(path, dllName));
Assert.That(changeSet, Is.EqualTo(321460));

var version = GetResult(bfv_get_version((param_string*) path2.DangerousGetHandle(), (param_string*) dllName2.DangerousGetHandle()));
var version = GetResult(bfv_get_version(path, dllName));
Assert.That(version, Is.EqualTo("e1.8.0"));

var versionType = GetResult(bfv_get_version_type((param_string*) path2.DangerousGetHandle(), (param_string*) dllName2.DangerousGetHandle()));
var versionType = GetResult(bfv_get_version_type(path, dllName));
Assert.That(versionType, Is.EqualTo(4));
});

Assert.That(DanglingAllocationsCount, Is.EqualTo(0));
Assert.That(LibraryAliveCount(), Is.EqualTo(0));
}
}
}
Loading

0 comments on commit 69acf87

Please sign in to comment.