From 37c0b984873dab15f317b406d726bca6b9f6a97c Mon Sep 17 00:00:00 2001 From: Nathan Crawford Date: Thu, 31 Mar 2016 20:49:39 -0400 Subject: [PATCH] Try to dispose IDisposable objects more diligently --- embroideryReader/GuiResources.cs | 32 +++++++ embroideryReader/embroideryReader.csproj | 1 + embroideryReader/frmMain.cs | 105 ++++++++++++++--------- 3 files changed, 97 insertions(+), 41 deletions(-) create mode 100644 embroideryReader/GuiResources.cs diff --git a/embroideryReader/GuiResources.cs b/embroideryReader/GuiResources.cs new file mode 100644 index 0000000..2e20938 --- /dev/null +++ b/embroideryReader/GuiResources.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; + +namespace embroideryReader +{ + class GuiResources + { + /// uiFlags: 0 - Count of GDI objects + /// uiFlags: 1 - Count of USER objects + /// - Win32 GDI objects (pens, brushes, fonts, palettes, regions, device contexts, bitmap headers) + /// - Win32 USER objects: + /// - WIN32 resources (accelerator tables, bitmap resources, dialog box templates, font resources, menu resources, raw data resources, string table entries, message table entries, cursors/icons) + /// - Other USER objects (windows, menus) + /// + [DllImport("User32")] + extern public static int GetGuiResources(IntPtr hProcess, int uiFlags); + + public static int GetGuiResourcesGDICount() + { + return GetGuiResources(Process.GetCurrentProcess().Handle, 0); + } + + public static int GetGuiResourcesUserCount() + { + return GetGuiResources(Process.GetCurrentProcess().Handle, 1); + } + } +} diff --git a/embroideryReader/embroideryReader.csproj b/embroideryReader/embroideryReader.csproj index 849620b..0e1247f 100644 --- a/embroideryReader/embroideryReader.csproj +++ b/embroideryReader/embroideryReader.csproj @@ -88,6 +88,7 @@ frmTextbox.cs + diff --git a/embroideryReader/frmMain.cs b/embroideryReader/frmMain.cs index d03fdc1..05d1455 100644 --- a/embroideryReader/frmMain.cs +++ b/embroideryReader/frmMain.cs @@ -244,6 +244,13 @@ namespace embroideryReader tempImage = destImage; } + // About to abandon the current DrawArea object, dispose it now + if(DrawArea != null) + { + DrawArea.Dispose(); + DrawArea = null; + } + // Add transparency grid if (settings.transparencyGridEnabled) { @@ -268,10 +275,15 @@ namespace embroideryReader } g.DrawImage(tempImage, 0, 0); + + // Done with tempImage + tempImage.Dispose(); + tempImage = null; } } else { + // Keeping the object tempImage was pointing at, so don't dispose it DrawArea = tempImage; } @@ -395,7 +407,11 @@ namespace embroideryReader private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { - MessageBox.Show(String.Format(translation.GetTranslatedString(Translation.StringID.ABOUT_MESSAGE), currentVersion())); // "EmbroideryReader version " + currentVersion() + ". This program reads and displays embroidery designs from .PES files." + string message = String.Format(translation.GetTranslatedString(Translation.StringID.ABOUT_MESSAGE), currentVersion()); // "EmbroideryReader version " + currentVersion() + ". This program reads and displays embroidery designs from .PES files." + message += Environment.NewLine + Environment.NewLine + "GUI GDI count: " + GuiResources.GetGuiResourcesGDICount(); + message += Environment.NewLine + "GUI USER count: " + GuiResources.GetGuiResourcesUserCount(); + message += Environment.NewLine + ".Net framework version: " + Environment.Version; + MessageBox.Show(message); } private void checkForUpdateToolStripMenuItem_Click(object sender, EventArgs e) @@ -511,8 +527,10 @@ namespace embroideryReader { float inchesPerMM = 0.03937007874015748031496062992126f; e.Graphics.ScaleTransform((float)(e.PageSettings.PrinterResolution.X * inchesPerMM * 0.01f), (float)(e.PageSettings.PrinterResolution.Y * inchesPerMM * 0.01f)); - Bitmap tempDrawArea = design.designToBitmap((float)settings.threadThickness, settings.filterStiches, settings.filterStitchesThreshold, e.PageSettings.PrinterResolution.X * inchesPerMM * 0.2f); - e.Graphics.DrawImage(tempDrawArea, 30, 30); + using (Bitmap tempDrawArea = design.designToBitmap((float)settings.threadThickness, settings.filterStiches, settings.filterStitchesThreshold, e.PageSettings.PrinterResolution.X * inchesPerMM * 0.2f)) + { + e.Graphics.DrawImage(tempDrawArea, 30, 30); + } } } @@ -527,12 +545,15 @@ namespace embroideryReader if (DrawArea != null) { Clipboard.Clear(); - Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); - Graphics tempGraph = Graphics.FromImage(temp); - tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height); - tempGraph.DrawImageUnscaled(DrawArea, 0, 0); - tempGraph.Dispose(); - Clipboard.SetImage(temp); + using (Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) + { + using (Graphics tempGraph = Graphics.FromImage(temp)) + { + tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height); + tempGraph.DrawImageUnscaled(DrawArea, 0, 0); + } + Clipboard.SetImage(temp); + } } } @@ -587,41 +608,43 @@ namespace embroideryReader { if (DrawArea != null) { - Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); - - Graphics tempGraph = Graphics.FromImage(temp); - tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height); - tempGraph.DrawImageUnscaled(DrawArea, 0, 0); - tempGraph.Dispose(); - saveFileDialog1.FileName = ""; - // "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif|All Files (*.*)|*.*" - saveFileDialog1.Filter = translation.GetTranslatedString(Translation.StringID.FILE_TYPE_BMP) + " (*.bmp)|*.bmp|" + - translation.GetTranslatedString(Translation.StringID.FILE_TYPE_PNG) + " (*.png)|*.png|" + - translation.GetTranslatedString(Translation.StringID.FILE_TYPE_JPG) + " (*.jpg)|*.jpg|" + - translation.GetTranslatedString(Translation.StringID.FILE_TYPE_GIF) + " (*.gif)|*.gif|" + - translation.GetTranslatedString(Translation.StringID.FILE_TYPE_TIFF) + " (*.tif)|*.tif|" + - translation.GetTranslatedString(Translation.StringID.FILE_TYPE_ALL) + " (*.*)|*.*"; - if (settings.lastSaveImageLocation != null) + using (Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { - saveFileDialog1.InitialDirectory = settings.lastSaveImageLocation; - } - if (saveFileDialog1.ShowDialog() == DialogResult.OK) - { - string filename = ""; - filename = saveFileDialog1.FileName; - System.Drawing.Imaging.ImageFormat format; - switch (System.IO.Path.GetExtension(filename).ToLower()) + using (Graphics tempGraph = Graphics.FromImage(temp)) { - case ".bmp": format = System.Drawing.Imaging.ImageFormat.Bmp; break; - case ".png": format = System.Drawing.Imaging.ImageFormat.Png; break; - case ".jpg": format = System.Drawing.Imaging.ImageFormat.Jpeg; break; - case ".gif": format = System.Drawing.Imaging.ImageFormat.Gif; break; - case ".tif": format = System.Drawing.Imaging.ImageFormat.Tiff; break; - default: format = System.Drawing.Imaging.ImageFormat.Bmp; break; + tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height); + tempGraph.DrawImageUnscaled(DrawArea, 0, 0); + } + saveFileDialog1.FileName = ""; + // "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif|All Files (*.*)|*.*" + saveFileDialog1.Filter = translation.GetTranslatedString(Translation.StringID.FILE_TYPE_BMP) + " (*.bmp)|*.bmp|" + + translation.GetTranslatedString(Translation.StringID.FILE_TYPE_PNG) + " (*.png)|*.png|" + + translation.GetTranslatedString(Translation.StringID.FILE_TYPE_JPG) + " (*.jpg)|*.jpg|" + + translation.GetTranslatedString(Translation.StringID.FILE_TYPE_GIF) + " (*.gif)|*.gif|" + + translation.GetTranslatedString(Translation.StringID.FILE_TYPE_TIFF) + " (*.tif)|*.tif|" + + translation.GetTranslatedString(Translation.StringID.FILE_TYPE_ALL) + " (*.*)|*.*"; + if (settings.lastSaveImageLocation != null) + { + saveFileDialog1.InitialDirectory = settings.lastSaveImageLocation; + } + if (saveFileDialog1.ShowDialog() == DialogResult.OK) + { + string filename = ""; + filename = saveFileDialog1.FileName; + System.Drawing.Imaging.ImageFormat format; + switch (System.IO.Path.GetExtension(filename).ToLower()) + { + case ".bmp": format = System.Drawing.Imaging.ImageFormat.Bmp; break; + case ".png": format = System.Drawing.Imaging.ImageFormat.Png; break; + case ".jpg": format = System.Drawing.Imaging.ImageFormat.Jpeg; break; + case ".gif": format = System.Drawing.Imaging.ImageFormat.Gif; break; + case ".tif": format = System.Drawing.Imaging.ImageFormat.Tiff; break; + default: format = System.Drawing.Imaging.ImageFormat.Bmp; break; + } + temp.Save(filename, format); + showStatus(translation.GetTranslatedString(Translation.StringID.IMAGE_SAVED), 5000); // "Image saved" + settings.lastSaveImageLocation = System.IO.Path.GetDirectoryName(filename); } - temp.Save(filename, format); - showStatus(translation.GetTranslatedString(Translation.StringID.IMAGE_SAVED), 5000); // "Image saved" - settings.lastSaveImageLocation = System.IO.Path.GetDirectoryName(filename); } } }