From d9c8d9cc1b9c24fe7dc874e99c32f4d51c95d364 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Tue, 27 Oct 2015 17:13:00 +0100 Subject: [PATCH] - .NET API: added Dispose method to IecConnection and ReportControlBlock classes --- dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs | 27 +++++++++++++--- .../IEC61850forCSharp/ReportControlBlock.cs | 27 +++++++++++----- dotnet/IEC61850forCSharp/Reporting.cs | 31 ++++++++++++++++++- dotnet/authenticate/Main.cs | 4 +++ dotnet/control/ControlExample.cs | 5 ++- dotnet/datasets/DataSetExample.cs | 5 ++- dotnet/example1/Main.cs | 5 ++- dotnet/example2/WriteValueExample.cs | 2 ++ dotnet/example3/Main.cs | 3 ++ dotnet/files/FileServicesExample.cs | 3 ++ dotnet/model_browsing/ModelBrowsing.cs | 2 ++ dotnet/report_new_dataset/Main.cs | 2 ++ dotnet/reporting/ReportingExample.cs | 9 ++++++ 13 files changed, 109 insertions(+), 16 deletions(-) diff --git a/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs b/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs index 73df640d..3c581979 100644 --- a/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs +++ b/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs @@ -259,12 +259,31 @@ namespace IEC61850 connection = IedConnection_create (); } + /// + /// Releases all resource used by the object. + /// + /// Call when you are finished using the . The + /// method leaves the in an unusable state. After + /// calling , you must release all references to the + /// so the garbage collector can reclaim the memory that the was occupying. + public void Dispose() + { + if (connection != IntPtr.Zero) { + cleanupRCBs (); + + IedConnection_destroy (connection); + + connection = IntPtr.Zero; + } + } + ~IedConnection () { - Console.WriteLine ("IedConnection destructor invoked"); - if (connection != IntPtr.Zero) - IedConnection_destroy(connection); - Console.WriteLine ("IedConnection destructor finished"); + if (connection != IntPtr.Zero) { + cleanupRCBs (); + + IedConnection_destroy (connection); + } } public IsoConnectionParameters GetConnectionParameters () diff --git a/dotnet/IEC61850forCSharp/ReportControlBlock.cs b/dotnet/IEC61850forCSharp/ReportControlBlock.cs index 57df5015..76cf65fc 100644 --- a/dotnet/IEC61850forCSharp/ReportControlBlock.cs +++ b/dotnet/IEC61850forCSharp/ReportControlBlock.cs @@ -203,7 +203,6 @@ namespace IEC61850 private void internalReportHandler (IntPtr parameter, IntPtr report) { - Console.WriteLine ("called internalReportHandler"); try { if (this.report == null) @@ -227,13 +226,25 @@ namespace IEC61850 this.objectReference = objectReference; } - ~ReportControlBlock() - { - Console.WriteLine ("Destructor invoked"); - //IedConnection_uninstallReportHandler(connection, objectReference); - //this.iedConnection = null; - Console.WriteLine ("Destructor finished"); - } + internal void DisposeInternal() + { + IedConnection_uninstallReportHandler(connection, objectReference); + } + + /// + /// Releases all resource used by the object. + /// + /// Call when you are finished using the . The + /// method leaves the in an unusable state. + /// After calling , you must release all references to the + /// so the garbage collector can reclaim the memory that the + /// was occupying. + public void Dispose() + { + DisposeInternal (); + + iedConnection.RemoveRCB (this); + } public string GetObjectReference () { diff --git a/dotnet/IEC61850forCSharp/Reporting.cs b/dotnet/IEC61850forCSharp/Reporting.cs index f61906a8..627116fe 100644 --- a/dotnet/IEC61850forCSharp/Reporting.cs +++ b/dotnet/IEC61850forCSharp/Reporting.cs @@ -24,6 +24,7 @@ using System; using System.Runtime.InteropServices; using IEC61850.Common; +using System.Collections.Generic; namespace IEC61850 { @@ -31,10 +32,38 @@ namespace IEC61850 { public partial class IedConnection { + + private List activeRCBs = null; + + private void cleanupRCBs() + { + if (activeRCBs != null) { + + foreach (ReportControlBlock rcb in activeRCBs) { + rcb.DisposeInternal (); + } + } + + } + public ReportControlBlock GetReportControlBlock (string rcbObjectReference) { - return new ReportControlBlock (rcbObjectReference, this, connection); + var newRCB = new ReportControlBlock (rcbObjectReference, this, connection); + + if (activeRCBs == null) + activeRCBs = new List (); + + activeRCBs.Add (newRCB); + + return newRCB; } + + internal void RemoveRCB(ReportControlBlock rcb) { + if (activeRCBs != null) { + activeRCBs.Remove (rcb); + } + } + } public enum ReasonForInclusion diff --git a/dotnet/authenticate/Main.cs b/dotnet/authenticate/Main.cs index 90f14cc8..afab9520 100644 --- a/dotnet/authenticate/Main.cs +++ b/dotnet/authenticate/Main.cs @@ -43,6 +43,10 @@ namespace authenticate { Console.WriteLine(e.Message); } + + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } \ No newline at end of file diff --git a/dotnet/control/ControlExample.cs b/dotnet/control/ControlExample.cs index aa3d0a5a..5504f317 100644 --- a/dotnet/control/ControlExample.cs +++ b/dotnet/control/ControlExample.cs @@ -91,7 +91,10 @@ namespace control catch (IedConnectionException e) { Console.WriteLine(e.Message); - } + } + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } diff --git a/dotnet/datasets/DataSetExample.cs b/dotnet/datasets/DataSetExample.cs index 5d502516..727e275f 100644 --- a/dotnet/datasets/DataSetExample.cs +++ b/dotnet/datasets/DataSetExample.cs @@ -72,7 +72,10 @@ namespace datasets catch (IedConnectionException e) { Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString()); - } + } + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } diff --git a/dotnet/example1/Main.cs b/dotnet/example1/Main.cs index 1d8abb23..24de9ebe 100644 --- a/dotnet/example1/Main.cs +++ b/dotnet/example1/Main.cs @@ -72,7 +72,10 @@ namespace example1 Console.WriteLine(e.Message); } - System.Threading.Thread.Sleep(2000); + System.Threading.Thread.Sleep(2000); + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/example2/WriteValueExample.cs b/dotnet/example2/WriteValueExample.cs index 1e150fc7..eb8b0228 100644 --- a/dotnet/example2/WriteValueExample.cs +++ b/dotnet/example2/WriteValueExample.cs @@ -39,6 +39,8 @@ namespace example2 Console.WriteLine("IED connection excepion: " + e.Message); } + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/example3/Main.cs b/dotnet/example3/Main.cs index 5f04ce98..5247bd31 100644 --- a/dotnet/example3/Main.cs +++ b/dotnet/example3/Main.cs @@ -43,6 +43,9 @@ namespace example3 { Console.WriteLine(e.Message); } + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/files/FileServicesExample.cs b/dotnet/files/FileServicesExample.cs index ccc94a0a..6aed827a 100644 --- a/dotnet/files/FileServicesExample.cs +++ b/dotnet/files/FileServicesExample.cs @@ -84,6 +84,9 @@ namespace files { Console.WriteLine(e.Message); } + + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/model_browsing/ModelBrowsing.cs b/dotnet/model_browsing/ModelBrowsing.cs index 40dc2673..1322118e 100644 --- a/dotnet/model_browsing/ModelBrowsing.cs +++ b/dotnet/model_browsing/ModelBrowsing.cs @@ -118,6 +118,8 @@ namespace model_browsing Console.WriteLine(e.Message); } + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/report_new_dataset/Main.cs b/dotnet/report_new_dataset/Main.cs index 06cd78e1..91644525 100644 --- a/dotnet/report_new_dataset/Main.cs +++ b/dotnet/report_new_dataset/Main.cs @@ -104,6 +104,8 @@ namespace report_new_dataset Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString()); } + // release all resources - do NOT use the object after this call!! + con.Dispose (); } } } diff --git a/dotnet/reporting/ReportingExample.cs b/dotnet/reporting/ReportingExample.cs index 506f283c..52b43b05 100644 --- a/dotnet/reporting/ReportingExample.cs +++ b/dotnet/reporting/ReportingExample.cs @@ -138,9 +138,18 @@ namespace reporting Thread.Sleep(10); } + /* Dispose the RCBs when you no longer need them */ + rcb1.Dispose(); + rcb2.Dispose(); + rcb3.Dispose(); + con.Abort (); + + con.Dispose(); } catch (IedConnectionException e) { Console.WriteLine ("Error: " + e.Message); + + con.Dispose (); } }