From efe4513d11b0a2f56c49e63f914accb327d6405e Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Sun, 18 Apr 2021 16:56:45 +0200 Subject: [PATCH] - .NET API: fully implemented dispose pattern in DataSet class --- dotnet/IEC61850forCSharp/DataSet.cs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/dotnet/IEC61850forCSharp/DataSet.cs b/dotnet/IEC61850forCSharp/DataSet.cs index 2fcb1040..d7304bc9 100644 --- a/dotnet/IEC61850forCSharp/DataSet.cs +++ b/dotnet/IEC61850forCSharp/DataSet.cs @@ -34,6 +34,11 @@ namespace IEC61850 /// This class is used to represent a data set. It is used to store the values /// of a data set. /// + /// + /// This class manages native resource. Take care that the finalizer/Dispose is not + /// called while running a method or the object is still in use by another object. + /// If in doubt please use the "using" statement. + /// public class DataSet : IDisposable { [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] @@ -108,21 +113,34 @@ namespace IEC61850 return ClientDataSet_getDataSetSize(nativeObject); } - public void Dispose() + private bool disposed = false; + + protected virtual void Dispose(bool disposing) { lock (this) { - if (nativeObject != IntPtr.Zero) + if (!disposed) { - ClientDataSet_destroy(nativeObject); - nativeObject = IntPtr.Zero; + if (nativeObject != IntPtr.Zero) + { + ClientDataSet_destroy(nativeObject); + nativeObject = IntPtr.Zero; + } + + disposed = true; } } } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + ~DataSet() { - Dispose(); + Dispose(false); } internal IntPtr getNativeInstance()