Skip to content

Commit

Permalink
Rework disposal of manage and unmanaged resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-el-sayed committed Dec 19, 2024
1 parent 544c699 commit ded5bbd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
28 changes: 22 additions & 6 deletions libs/UGridNET/dll/src/UGridBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,36 @@ protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
// Free unmanaged memory of IntPtrs belonging to the different entities
FreeUnmanagedMemoryInTopologyLists();

// Close the file
Close();

if (disposing)
{
// Free unmanaged memory of IntPtrs belonging to the different entities
FreeUnmanagedMemory();

// Close the file
Close();
DisposeAndClearTopologyLists();
}

disposed = true;
}
}

private void FreeUnmanagedMemory()
private void DisposeAndClearLists<T>(List<T> list) where T : IDisposable
{
list.ForEach(item => item.Dispose());
list.Clear();
}

private void DisposeAndClearTopologyLists()
{
DisposeAndClearLists(mesh1DList);
DisposeAndClearLists(mesh2DList);
DisposeAndClearLists(contactsList);
DisposeAndClearLists(network1DList);
}

private void FreeUnmanagedMemoryInTopologyLists()
{
mesh1DList.ForEach(item => item.Free());
mesh2DList.ForEach(item => item.Free());
Expand Down
31 changes: 0 additions & 31 deletions libs/UGridNET/dll/src/UGridReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace UGridNET

public sealed class UGridReader : UGridBase
{
private bool disposed = false;

public UGridReader(string path) : base(path, 0)
{
try
Expand All @@ -30,35 +28,6 @@ public UGridReader(string path) : base(path, 0)
}
}

~UGridReader()
{
// Cleanup in case Dispose wasn't called
Dispose(false);
}

public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
base.Dispose();
}

protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// clean up
}

disposed = true;

base.Dispose(disposing);
}
}


private void ReadMesh1D()
{
int count = 0;
Expand Down

0 comments on commit ded5bbd

Please sign in to comment.