From 65e6368cbe7eeddd02bbd43ae5159ab4b762165f Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 8 Jan 2019 15:12:45 +0200 Subject: Fixed color conversion memory leak by dynamically loading the library. (LoadLibrary, FreeLibrary) --- Software/DB/Tango.mdf | Bin 75497472 -> 75497472 bytes Software/DB/Tango_log.ldf | Bin 22675456 -> 22675456 bytes .../ColorConversion/TangoColorConverter.cs | 55 ++++++++++++--------- Software/Visual_Studio/Tango.PMR/NativePMR.cs | 19 +++---- 4 files changed, 42 insertions(+), 32 deletions(-) (limited to 'Software') diff --git a/Software/DB/Tango.mdf b/Software/DB/Tango.mdf index 2468d8559..fda449446 100644 Binary files a/Software/DB/Tango.mdf and b/Software/DB/Tango.mdf differ diff --git a/Software/DB/Tango_log.ldf b/Software/DB/Tango_log.ldf index de658cfa8..ec0a2e6b6 100644 Binary files a/Software/DB/Tango_log.ldf and b/Software/DB/Tango_log.ldf differ diff --git a/Software/Visual_Studio/Tango.BL/ColorConversion/TangoColorConverter.cs b/Software/Visual_Studio/Tango.BL/ColorConversion/TangoColorConverter.cs index 776fa7448..e94dd851b 100644 --- a/Software/Visual_Studio/Tango.BL/ColorConversion/TangoColorConverter.cs +++ b/Software/Visual_Studio/Tango.BL/ColorConversion/TangoColorConverter.cs @@ -17,16 +17,31 @@ namespace Tango.BL.ColorConversion { public static class TangoColorConverter { - [DllImport("Tango.ColorLib.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Convert")] - private static extern int Convert(IntPtr data, int size, ref IntPtr output); + static class NativeMethods + { + [DllImport("kernel32.dll")] + public static extern IntPtr LoadLibrary(string dllToLoad); - public static ConversionOutput GetSuggestions(BrushStop brushStop) + [DllImport("kernel32.dll")] + public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); + + [DllImport("kernel32.dll")] + public static extern bool FreeLibrary(IntPtr hModule); + } + + private static ConversionOutput Convert(ConversionInput conversionInput) { - ConversionInput conversionInput = CreateConversionInput(brushStop); + IntPtr pDll = NativeMethods.LoadLibrary(@"Tango.ColorLib.dll"); + IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "Convert"); + NativeMethodDelegate convert = (NativeMethodDelegate)Marshal.GetDelegateForFunctionPointer( + pAddressOfFunctionToCall, + typeof(NativeMethodDelegate)); - NativePMR nativePMR = new NativePMR(Convert); + NativePMR nativePMR = new NativePMR(convert); ConversionOutput output = nativePMR.Invoke(conversionInput); + bool result = NativeMethods.FreeLibrary(pDll); + if (output.HasError) { throw new ExternalException($"Color Conversion Error: {output.ErrorMessage}!"); @@ -35,16 +50,16 @@ namespace Tango.BL.ColorConversion return output; } - public static ConversionOutput GetSuggestions(ConversionInput conversionInput) + public static ConversionOutput GetSuggestions(BrushStop brushStop) { - NativePMR nativePMR = new NativePMR(Convert); - ConversionOutput output = nativePMR.Invoke(conversionInput); - - if (output.HasError) - { - throw LogManager.Default.Log(new ExternalException($"Color Conversion Error: {output.ErrorMessage}!"), LogCategory.Warning); - } + ConversionInput conversionInput = CreateConversionInput(brushStop); + ConversionOutput output = Convert(conversionInput); + return output; + } + public static ConversionOutput GetSuggestions(ConversionInput conversionInput) + { + ConversionOutput output = Convert(conversionInput); return output; } @@ -59,9 +74,9 @@ namespace Tango.BL.ColorConversion conversionInput.InputCoordinates.Green = Math.Max((int)color.G, 1); conversionInput.InputCoordinates.Blue = Math.Max((int)color.B, 1); - conversionInput.ThreadL = 92.1815;//brushStop.Segment.Job.Rml.MediaColor.L; - conversionInput.ThreadA = 2.2555;//brushStop.Segment.Job.Rml.MediaColor.A; - conversionInput.ThreadB = -10.9325;//brushStop.Segment.Job.Rml.MediaColor.B; + conversionInput.ThreadL = 92.1815;//brushStop.Segment.Job.Rml.WhitePoint.L; + conversionInput.ThreadA = 2.2555;//brushStop.Segment.Job.Rml.WhitePoint.A; + conversionInput.ThreadB = -10.9325;//brushStop.Segment.Job.Rml.WhitePoint.B; conversionInput.ForwardData = ByteString.CopyFrom(job.Rml.Ccts.FirstOrDefault().ForwardData); @@ -104,13 +119,7 @@ namespace Tango.BL.ColorConversion }); } - NativePMR nativePMR = new NativePMR(Convert); - ConversionOutput output = nativePMR.Invoke(conversionInput); - - if (output.HasError) - { - throw LogManager.Default.Log(new ExternalException($"Color Conversion Error: {output.ErrorMessage}!"), LogCategory.Warning); - } + ConversionOutput output = Convert(conversionInput); return output; } diff --git a/Software/Visual_Studio/Tango.PMR/NativePMR.cs b/Software/Visual_Studio/Tango.PMR/NativePMR.cs index 14e740656..5666fbeb0 100644 --- a/Software/Visual_Studio/Tango.PMR/NativePMR.cs +++ b/Software/Visual_Studio/Tango.PMR/NativePMR.cs @@ -9,6 +9,16 @@ using System.Runtime.ExceptionServices; namespace Tango.PMR { + /// + /// Represents a standard C++ protobuf method delegate. + /// + /// The request array. + /// Size of the request array. + /// The result array. + /// The size of the result array. + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate int NativeMethodDelegate(IntPtr requestArray, int requestArraySize, ref IntPtr resultArray); + /// /// Represents a PMR wrapper for invoking native protobuf supported methods. /// @@ -27,15 +37,6 @@ namespace Tango.PMR _nativeMethod = nativeMethod; } - /// - /// Represents a standard C++ protobuf method delegate. - /// - /// The request array. - /// Size of the request array. - /// The result array. - /// The size of the result array. - public delegate int NativeMethodDelegate(IntPtr requestArray, int requestArraySize, ref IntPtr resultArray); - /// /// Invokes the native method with the specified request. /// -- cgit v1.3.1