Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup management of managed and unmanaged resources #101

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
27 changes: 24 additions & 3 deletions nng.NET/AsyncContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,24 @@ protected void HandleFailedSend()
protected object sync = new object();

#region IDisposable
public void Dispose()
public void Dispose() => Dispose(true);

protected virtual void Dispose(bool disposing)
{
Aio?.Dispose();
if (disposed)
return;

if (disposing)
{
Aio?.Dispose();
}

// No unmanaged resources to cleanup

disposed = true;
}

bool disposed = false;
#endregion
}

Expand Down Expand Up @@ -253,12 +267,19 @@ protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
var _ = nng_ctx_close(NativeNngStruct);
// No managed resources to dispose
}

var _ = nng_ctx_close(NativeNngStruct);

disposed = true;
}

~NngCtx() => Dispose(false);

bool disposed = false;
#endregion
}
Expand Down
9 changes: 8 additions & 1 deletion nng.NET/Dialer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
int _ = nng_dialer_close(NativeNngStruct);
// No managed resources to dispose
}

int _ = nng_dialer_close(NativeNngStruct);

disposed = true;
}

~NngDialer() => Dispose(false);

bool disposed = false;
#endregion

Expand Down
9 changes: 8 additions & 1 deletion nng.NET/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,19 @@ protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
int _ = nng_listener_close(NativeNngStruct);
// No managed resources to dispose
}

int _ = nng_listener_close(NativeNngStruct);

disposed = true;
}

~NngListener() => Dispose(false);

bool disposed = false;
#endregion

Expand Down
13 changes: 10 additions & 3 deletions nng.NET/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Take()
Ptr = default;
Length = default;
}

#region IDisposable
public void Dispose()
{
Expand All @@ -52,13 +52,20 @@ protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
if (Ptr != IntPtr.Zero)
nng_free(Ptr, Length);
// No managed resources to dispose
}

if (Ptr != IntPtr.Zero)
nng_free(Ptr, Length);

disposed = true;
}

~NngAlloc() => Dispose(false);

bool disposed = false;
#endregion
}
Expand Down
9 changes: 8 additions & 1 deletion nng.NET/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,19 @@ protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
int _ = nng_close(NativeNngStruct);
// No managed resources to dispose
}

int _ = nng_close(NativeNngStruct);

disposed = true;
}

~NngSocket() => Dispose(false);

bool disposed = false;
#endregion
}
Expand Down
14 changes: 10 additions & 4 deletions nng.NET/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public static NngResult<IStatRoot> GetStatSnapshot()
var res = nng_stats_get(out nng_stat statsp);
return NngResult<IStatRoot>.OkThen(res, () => new StatRoot { NativeNngStruct = statsp });
}
}

public class StatRoot : NngStat, IStatRoot
{
#region IDisposable
public void Dispose()
{
Expand All @@ -52,14 +49,23 @@ protected virtual void Dispose(bool disposing)
return;
if (disposing)
{
nng_stats_free(NativeNngStruct);
// No managed resources to dispose
}
nng_stats_free(NativeNngStruct);
disposed = true;
}

~NngStat() => Dispose(false);

bool disposed = false;
#endregion
}

public class StatRoot : NngStat, IStatRoot
{

}

public class StatChild : NngStat, IStatChild
{
public static IStatChild Create(nng_stat stat)
Expand Down
6 changes: 4 additions & 2 deletions nng.NET/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ protected virtual void Dispose(bool disposing)
return;
if (disposing)
{
if (Ptr != IntPtr.Zero)
nng_strfree(Ptr);
// No managed resources to dispose
}
if (Ptr != IntPtr.Zero)
nng_strfree(Ptr);
disposed = true;
}
~NngString() => Dispose(false);
bool disposed = false;
#endregion
}
Expand Down