diff --git a/Greenshot/Controls/Pipette.cs b/Greenshot/Controls/Pipette.cs index d5aafd488..59016928a 100644 --- a/Greenshot/Controls/Pipette.cs +++ b/Greenshot/Controls/Pipette.cs @@ -68,13 +68,6 @@ namespace Greenshot.Controls { } } - /// - /// Destructor - /// - ~Pipette() { - Dispose(false); - } - /// /// The bulk of the clean-up code is implemented in Dispose(bool) /// diff --git a/Greenshot/Drawing/CursorContainer.cs b/Greenshot/Drawing/CursorContainer.cs index 1166cd059..d4a3ac63c 100644 --- a/Greenshot/Drawing/CursorContainer.cs +++ b/Greenshot/Drawing/CursorContainer.cs @@ -56,21 +56,8 @@ namespace Greenshot.Drawing { get { return cursor; } } - /** - * Destructor - */ - ~CursorContainer() { - Dispose(false); - } - - /** - * The public accessible Dispose - * Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - */ - public new void Dispose() { - Dispose(true); + public override void Dispose() { base.Dispose(); - GC.SuppressFinalize(this); } // The bulk of the clean-up code is implemented in Dispose(bool) @@ -79,13 +66,14 @@ namespace Greenshot.Drawing { * This Dispose is called from the Dispose and the Destructor. * When disposing==true all non-managed resources should be freed too! */ - protected virtual void Dispose(bool disposing) { + protected override void Dispose(bool disposing) { if (disposing) { if (cursor != null) { cursor.Dispose(); } } cursor = null; + base.Dispose(disposing); } public void Load(string filename) { diff --git a/Greenshot/Drawing/DrawableContainer.cs b/Greenshot/Drawing/DrawableContainer.cs index 02bb01d92..566af1014 100644 --- a/Greenshot/Drawing/DrawableContainer.cs +++ b/Greenshot/Drawing/DrawableContainer.cs @@ -46,7 +46,28 @@ namespace Greenshot.Drawing { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(DrawableContainer)); protected static readonly EditorConfiguration editorConfig = IniConfig.GetIniSection(); private bool isMadeUndoable = false; - + + public virtual void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + for (int i = 0; i < grippers.Length; i++) { + grippers[i].Dispose(); + grippers[i] = null; + } + + FieldAggregator aggProps = parent.FieldAggregator; + aggProps.UnbindElement(this); + } + } + + ~DrawableContainer() { + Dispose(false); + } + [NonSerialized] private PropertyChangedEventHandler propertyChanged; public event PropertyChangedEventHandler PropertyChanged { @@ -335,15 +356,6 @@ namespace Greenshot.Drawing { } } - public virtual void Dispose() { - for(int i=0; i - /// Destructor - /// - ~FreehandContainer() { - Dispose(false); - } - - /// - /// The public accessible Dispose - /// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - /// - public new void Dispose() { - Dispose(true); - base.Dispose(); - GC.SuppressFinalize(this); - } - /// /// This Dispose is called from the Dispose and the Destructor. /// /// When disposing==true all non-managed resources should be freed too! - protected virtual void Dispose(bool disposing) { + protected override void Dispose(bool disposing) { if (disposing) { if (freehandPath != null) { freehandPath.Dispose(); } } freehandPath = null; + base.Dispose(disposing); } /// diff --git a/Greenshot/Drawing/IconContainer.cs b/Greenshot/Drawing/IconContainer.cs index 5b2c22786..10e574214 100644 --- a/Greenshot/Drawing/IconContainer.cs +++ b/Greenshot/Drawing/IconContainer.cs @@ -55,36 +55,18 @@ namespace Greenshot.Drawing { get { return icon; } } - /** - * Destructor - */ - ~IconContainer() { - Dispose(false); - } - - /** - * The public accessible Dispose - * Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - */ - public new void Dispose() { - Dispose(true); - base.Dispose(); - GC.SuppressFinalize(this); - } - - // The bulk of the clean-up code is implemented in Dispose(bool) - /** * This Dispose is called from the Dispose and the Destructor. * When disposing==true all non-managed resources should be freed too! */ - protected virtual void Dispose(bool disposing) { + protected override void Dispose(bool disposing) { if (disposing) { if (icon != null) { icon.Dispose(); } } icon = null; + base.Dispose(disposing); } public void Load(string filename) { diff --git a/Greenshot/Drawing/ImageContainer.cs b/Greenshot/Drawing/ImageContainer.cs index 97acc03b2..fa8077168 100644 --- a/Greenshot/Drawing/ImageContainer.cs +++ b/Greenshot/Drawing/ImageContainer.cs @@ -110,30 +110,13 @@ namespace Greenshot.Drawing { get { return image; } } - /// - /// Destructor - /// - ~ImageContainer() { - Dispose(false); - } - - /// - /// The public accessible Dispose - /// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - /// - public new void Dispose() { - Dispose(true); - base.Dispose(); - GC.SuppressFinalize(this); - } - /// /// The bulk of the clean-up code is implemented in Dispose(bool) /// This Dispose is called from the Dispose and the Destructor. /// When disposing==true all non-managed resources should be freed too! /// /// - protected virtual void Dispose(bool disposing) { + protected override void Dispose(bool disposing) { if (disposing) { if (image != null) { image.Dispose(); @@ -144,6 +127,7 @@ namespace Greenshot.Drawing { } image = null; shadowBitmap = null; + base.Dispose(disposing); } /// diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index 8f654f6b0..25929fc02 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -320,7 +320,9 @@ namespace Greenshot.Drawing { set { drawingMode = value; if (drawingModeChanged != null) { - drawingModeChanged.Invoke(this, drawingMode); + SurfaceDrawingModeEventArgs eventArgs = new SurfaceDrawingModeEventArgs(); + eventArgs.DrawingMode = drawingMode; + drawingModeChanged.Invoke(this, eventArgs); } DeselectAllElements(); CreateUndrawnElement(); @@ -437,28 +439,35 @@ namespace Greenshot.Drawing { /// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice /// public new void Dispose() { - Count--; - LOG.Debug("Disposing surface!"); - if (buffer != null) { - buffer.Dispose(); - buffer = null; - } - if (transparencyBackgroundBrush != null) { - transparencyBackgroundBrush.Dispose(); - transparencyBackgroundBrush = null; - } - - // Cleanup undo/redo stacks - while (undoStack != null && undoStack.Count > 0) { - undoStack.Pop().Dispose(); - } - while (redoStack != null && redoStack.Count > 0) { - redoStack.Pop().Dispose(); - } + Dispose(true); base.Dispose(); GC.SuppressFinalize(this); } + protected override void Dispose(bool disposing) { + if (disposing) { + Count--; + LOG.Debug("Disposing surface!"); + if (buffer != null) { + buffer.Dispose(); + buffer = null; + } + if (transparencyBackgroundBrush != null) { + transparencyBackgroundBrush.Dispose(); + transparencyBackgroundBrush = null; + } + + // Cleanup undo/redo stacks + while (undoStack != null && undoStack.Count > 0) { + undoStack.Pop().Dispose(); + } + while (redoStack != null && redoStack.Count > 0) { + redoStack.Pop().Dispose(); + } + } + base.Dispose(disposing); + } + /// /// Undo the last action /// @@ -833,7 +842,7 @@ namespace Greenshot.Drawing { SetImage(newImage, false); Invalidate(); if (surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, newImage.Size))) { - surfaceSizeChanged(this); + surfaceSizeChanged(this, null); } } } finally { @@ -911,7 +920,7 @@ namespace Greenshot.Drawing { SetImage(tmpImage, false); elements.MoveBy(offset.X, offset.Y); if (surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, tmpImage.Size))) { - surfaceSizeChanged(this); + surfaceSizeChanged(this, null); } Invalidate(); return true; @@ -929,7 +938,7 @@ namespace Greenshot.Drawing { SetImage(previous, false); elements.MoveBy(offset.X, offset.Y); if (surfaceSizeChanged != null) { - surfaceSizeChanged(this); + surfaceSizeChanged(this, null); } Invalidate(); } @@ -1295,7 +1304,9 @@ namespace Greenshot.Drawing { } selectedElements.Clear(); if (movingElementChanged != null) { - movingElementChanged(this, selectedElements); + SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs(); + eventArgs.Elements = selectedElements; + movingElementChanged(this, eventArgs); } } } @@ -1414,7 +1425,9 @@ namespace Greenshot.Drawing { selectedElements.Remove(container); FieldAggregator.UnbindElement(container); if (movingElementChanged != null) { - movingElementChanged(this, selectedElements); + SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs(); + eventArgs.Elements = selectedElements; + movingElementChanged(this, eventArgs); } } @@ -1432,7 +1445,9 @@ namespace Greenshot.Drawing { FieldAggregator.UnbindElement(element); } if (movingElementChanged != null) { - movingElementChanged(this, selectedElements); + SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs(); + eventArgs.Elements = selectedElements; + movingElementChanged(this, eventArgs); } } } @@ -1448,7 +1463,9 @@ namespace Greenshot.Drawing { container.Selected = true; FieldAggregator.BindElement(container); if (movingElementChanged != null) { - movingElementChanged(this, selectedElements); + SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs(); + eventArgs.Elements = selectedElements; + movingElementChanged(this, eventArgs); } container.Invalidate(); } diff --git a/Greenshot/Drawing/TextContainer.cs b/Greenshot/Drawing/TextContainer.cs index adb14c506..e39036ff4 100644 --- a/Greenshot/Drawing/TextContainer.cs +++ b/Greenshot/Drawing/TextContainer.cs @@ -94,39 +94,23 @@ namespace Greenshot.Drawing { Init(); UpdateFormat(); } - - /** - * Destructor - */ - ~TextContainer() { - Dispose(false); - } - /** - * The public accessible Dispose - * Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - */ - public override void Dispose() { - Dispose(true); - base.Dispose(); - GC.SuppressFinalize(this); - } - - /** - * This Dispose is called from the Dispose and the Destructor. - * When disposing==true all non-managed resources should be freed too! - */ - protected virtual void Dispose(bool disposing) { + protected override void Dispose(bool disposing) { if (disposing) { - if (textBox != null) { - textBox.Dispose(); - } if (font != null) { font.Dispose(); + font = null; + } + if (stringFormat != null) { + stringFormat.Dispose(); + stringFormat = null; + } + if (textBox != null) { + textBox.Dispose(); + textBox = null; } } - textBox = null; - font = null; + base.Dispose(disposing); } private void Init() { diff --git a/Greenshot/Forms/AboutForm.cs b/Greenshot/Forms/AboutForm.cs index 156e20e0b..416c3220a 100644 --- a/Greenshot/Forms/AboutForm.cs +++ b/Greenshot/Forms/AboutForm.cs @@ -33,6 +33,7 @@ using Greenshot.Configuration; using GreenshotPlugin.Core; using Greenshot.IniFile; using GreenshotPlugin.Controls; +using System.Security.Permissions; namespace Greenshot { /// @@ -281,6 +282,7 @@ namespace Greenshot { /// /// /// + [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { try { switch (keyData) { diff --git a/Greenshot/Forms/ImageEditorForm.cs b/Greenshot/Forms/ImageEditorForm.cs index a47d7bbf1..e3f1ade4b 100644 --- a/Greenshot/Forms/ImageEditorForm.cs +++ b/Greenshot/Forms/ImageEditorForm.cs @@ -144,7 +144,7 @@ namespace Greenshot { surface.SurfaceSizeChanged += new SurfaceSizeChangeEventHandler(SurfaceSizeChanged); surface.SurfaceMessage += new SurfaceMessageEventHandler(SurfaceMessageReceived); surface.FieldAggregator.FieldChanged += new FieldChangedEventHandler(FieldAggregatorFieldChanged); - SurfaceSizeChanged(this.Surface); + SurfaceSizeChanged(this.Surface, null); bindFieldControls(); refreshEditorControls(); @@ -340,7 +340,7 @@ namespace Greenshot { /// This is called when the size of the surface chances, used for resizing and displaying the size information /// /// - private void SurfaceSizeChanged(object source) { + private void SurfaceSizeChanged(object sender, EventArgs e) { if (editorConfiguration.MatchSizeToCapture) { // Set editor's initial size to the size of the surface plus the size of the chrome Size imageSize = this.Surface.Image.Size; @@ -353,7 +353,7 @@ namespace Greenshot { this.Size = new Size(newWidth, newHeight); } dimensionsLabel.Text = this.Surface.Image.Width + "x" + this.Surface.Image.Height; - ImageEditorFormResize(source, new EventArgs()); + ImageEditorFormResize(sender, new EventArgs()); } private void ReloadConfiguration(object source, FileSystemEventArgs e) { @@ -391,8 +391,8 @@ namespace Greenshot { this.Text = Path.GetFileName(fullpath) + " - " + Language.GetString(LangKey.editor_title); } - void surface_DrawingModeChanged(object source, DrawingModes drawingMode) { - switch (drawingMode) { + void surface_DrawingModeChanged(object source, SurfaceDrawingModeEventArgs eventArgs) { + switch (eventArgs.DrawingMode) { case DrawingModes.None: SetButtonChecked(btnCursor); break; diff --git a/Greenshot/Forms/MainForm.Designer.cs b/Greenshot/Forms/MainForm.Designer.cs index bba254d0c..61a02d9ff 100644 --- a/Greenshot/Forms/MainForm.Designer.cs +++ b/Greenshot/Forms/MainForm.Designer.cs @@ -34,6 +34,9 @@ namespace Greenshot { if (components != null) { components.Dispose(); } + if (copyData != null) { + copyData.Dispose(); + } } base.Dispose(disposing); } diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index eaf2064f0..3a400ef05 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -330,7 +330,7 @@ namespace Greenshot { } catch (ArgumentException ex) { // Added for Bug #1420, this doesn't solve the issue but maybe the user can do something with it. ex.Data.Add("more information here", "http://support.microsoft.com/kb/943140"); - throw ex; + throw; } this.notifyIcon.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); @@ -1210,7 +1210,7 @@ namespace Greenshot { } catch (Exception ex) { // Make sure we show what we tried to open in the exception ex.Data.Add("path", path); - throw ex; + throw; } break; case ClickActions.OPEN_LAST_IN_EDITOR: @@ -1252,7 +1252,7 @@ namespace Greenshot { } catch (Exception e) { // Make sure we show what we tried to open in the exception e.Data.Add("path", path); - throw e; + throw; } } diff --git a/Greenshot/Helpers/CopyData.cs b/Greenshot/Helpers/CopyData.cs index 8f707a992..b877fef9f 100644 --- a/Greenshot/Helpers/CopyData.cs +++ b/Greenshot/Helpers/CopyData.cs @@ -91,7 +91,6 @@ namespace Greenshot.Helpers { #region Member Variables private CopyDataChannels channels = null; - private bool disposed = false; #endregion /// @@ -156,15 +155,18 @@ namespace Greenshot.Helpers { } } + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + /// /// Clears up any resources associated with this object. /// - public void Dispose() { - if (!disposed) { + protected virtual void Dispose(bool disposing) { + if (disposing) { channels.Clear(); channels = null; - disposed = true; - GC.SuppressFinalize(this); } } @@ -360,15 +362,15 @@ namespace Greenshot.Helpers { /// public class CopyDataChannel : IDisposable { #region Unmanaged Code - [DllImport("user32", CharSet=CharSet.Auto, SetLastError = true)] - private extern static int GetProp(IntPtr hwnd, string lpString); - [DllImport("user32", CharSet=CharSet.Auto, SetLastError = true)] - private extern static int SetProp(IntPtr hwnd, string lpString, int hData); - [DllImport("user32", CharSet=CharSet.Auto, SetLastError = true)] - private extern static int RemoveProp(IntPtr hwnd, string lpString); - - [DllImport("user32", CharSet=CharSet.Auto, SetLastError = true)] - private extern static int SendMessage(IntPtr hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam); + [DllImport("user32", CharSet=CharSet.Unicode, SetLastError = true)] + private extern static IntPtr GetProp(IntPtr hwnd, string lpString); + [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] + private extern static IntPtr SetProp(IntPtr hwnd, string lpString, int hData); + [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] + private extern static IntPtr RemoveProp(IntPtr hwnd, string lpString); + + [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] + private extern static IntPtr SendMessage(IntPtr hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam); [StructLayout(LayoutKind.Sequential)] private struct COPYDATASTRUCT { @@ -382,7 +384,6 @@ namespace Greenshot.Helpers { #region Member Variables private string channelName = ""; - private bool disposed = false; private NativeWindow owner = null; private bool recreateChannel = false; #endregion @@ -406,10 +407,6 @@ namespace Greenshot.Helpers { public int Send(object obj) { int recipients = 0; - if (disposed) { - throw new InvalidOperationException("Object has been disposed"); - } - if (recreateChannel) { // handle has changed addChannel(); @@ -446,12 +443,12 @@ namespace Greenshot.Helpers { // the channel: foreach(WindowDetails window in WindowDetails.GetAllWindows()) { if (!window.Handle.Equals(this.owner.Handle)) { - if (GetProp(window.Handle, this.channelName) != 0) { + if (GetProp(window.Handle, this.channelName) != IntPtr.Zero) { COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = dataSize; cds.dwData = IntPtr.Zero; cds.lpData = ptrData; - int res = SendMessage(window.Handle, WM_COPYDATA, (int)owner.Handle, ref cds); + SendMessage(window.Handle, WM_COPYDATA, (int)owner.Handle, ref cds); recipients += (Marshal.GetLastWin32Error() == 0 ? 1 : 0); } } @@ -487,17 +484,20 @@ namespace Greenshot.Helpers { recreateChannel = true; } + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + /// /// Clears up any resources associated with this channel. /// - public void Dispose() { - if (!disposed) { + protected virtual void Dispose(bool disposing) { + if (disposing) { if (channelName.Length > 0) { removeChannel(); } channelName = ""; - disposed = true; - GC.SuppressFinalize(this); } } diff --git a/Greenshot/Helpers/MailHelper.cs b/Greenshot/Helpers/MailHelper.cs index 207ca3649..ab51a0e41 100644 --- a/Greenshot/Helpers/MailHelper.cs +++ b/Greenshot/Helpers/MailHelper.cs @@ -457,20 +457,8 @@ namespace Greenshot.Helpers { } #endregion Constructors - - #region Constants - - public const int MAPI_LOGON_UI = 0x1; - - #endregion Constants - - #region APIs - - [DllImport("MAPI32.DLL", CharSet = CharSet.Ansi, SetLastError = true)] - public static extern int MAPILogon(IntPtr hwnd, string prf, string pw, int flg, int rsv, ref IntPtr sess); - - #endregion APIs - + + #region Structs [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] diff --git a/Greenshot/Helpers/PluginHelper.cs b/Greenshot/Helpers/PluginHelper.cs index 1ba13f85c..2cefbb29b 100644 --- a/Greenshot/Helpers/PluginHelper.cs +++ b/Greenshot/Helpers/PluginHelper.cs @@ -73,6 +73,7 @@ namespace Greenshot.Helpers { public void Shutdown() { foreach(IGreenshotPlugin plugin in plugins.Values) { plugin.Shutdown(); + plugin.Dispose(); } plugins.Clear(); } diff --git a/Greenshot/Helpers/PrintHelper.cs b/Greenshot/Helpers/PrintHelper.cs index bf805ec30..2ea58a5ea 100644 --- a/Greenshot/Helpers/PrintHelper.cs +++ b/Greenshot/Helpers/PrintHelper.cs @@ -72,7 +72,7 @@ namespace Greenshot.Helpers { * This Dispose is called from the Dispose and the Destructor. * When disposing==true all non-managed resources should be freed too! */ - protected void Dispose(bool disposing) { + protected virtual void Dispose(bool disposing) { if (disposing) { if (image != null) { image.Dispose(); diff --git a/Greenshot/Helpers/SoundHelper.cs b/Greenshot/Helpers/SoundHelper.cs index b04ad0e29..9c04505da 100644 --- a/Greenshot/Helpers/SoundHelper.cs +++ b/Greenshot/Helpers/SoundHelper.cs @@ -43,24 +43,26 @@ namespace Greenshot.Helpers { private static byte[] soundBuffer = null; public static void Initialize() { - try { - ResourceManager resources = new ResourceManager("Greenshot.Sounds", Assembly.GetExecutingAssembly()); - soundBuffer = (byte[])resources.GetObject("camera"); + if (gcHandle == null) { + try { + ResourceManager resources = new ResourceManager("Greenshot.Sounds", Assembly.GetExecutingAssembly()); + soundBuffer = (byte[])resources.GetObject("camera"); - if (conf.NotificationSound != null && conf.NotificationSound.EndsWith(".wav")) { - try { - if (File.Exists(conf.NotificationSound)) { - soundBuffer = File.ReadAllBytes(conf.NotificationSound); - } - } catch (Exception ex) { - LOG.WarnFormat("couldn't load {0}: {1}", conf.NotificationSound, ex.Message); - } - } - // Pin sound so it can't be moved by the Garbage Collector, this was the cause for the bad sound - gcHandle = GCHandle.Alloc(soundBuffer, GCHandleType.Pinned); - } catch (Exception e) { - LOG.Error("Error initializing.", e); - } + if (conf.NotificationSound != null && conf.NotificationSound.EndsWith(".wav")) { + try { + if (File.Exists(conf.NotificationSound)) { + soundBuffer = File.ReadAllBytes(conf.NotificationSound); + } + } catch (Exception ex) { + LOG.WarnFormat("couldn't load {0}: {1}", conf.NotificationSound, ex.Message); + } + } + // Pin sound so it can't be moved by the Garbage Collector, this was the cause for the bad sound + gcHandle = GCHandle.Alloc(soundBuffer, GCHandleType.Pinned); + } catch (Exception e) { + LOG.Error("Error initializing.", e); + } + } } public static void Play() { diff --git a/Greenshot/Memento/AddElementMemento.cs b/Greenshot/Memento/AddElementMemento.cs index 6448f7086..785e017ed 100644 --- a/Greenshot/Memento/AddElementMemento.cs +++ b/Greenshot/Memento/AddElementMemento.cs @@ -37,6 +37,14 @@ namespace Greenshot.Memento { } public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) { } + drawableContainer = null; + surface = null; } public LangKey ActionLanguageKey { diff --git a/Greenshot/Memento/ChangeFieldHolderMemento.cs b/Greenshot/Memento/ChangeFieldHolderMemento.cs index df00aec91..8004f00c9 100644 --- a/Greenshot/Memento/ChangeFieldHolderMemento.cs +++ b/Greenshot/Memento/ChangeFieldHolderMemento.cs @@ -40,6 +40,13 @@ namespace Greenshot.Memento { } public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) { } + drawableContainer = null; } public LangKey ActionLanguageKey { diff --git a/Greenshot/Memento/DeleteElementMemento.cs b/Greenshot/Memento/DeleteElementMemento.cs index 9c9ebab01..991235879 100644 --- a/Greenshot/Memento/DeleteElementMemento.cs +++ b/Greenshot/Memento/DeleteElementMemento.cs @@ -37,9 +37,16 @@ namespace Greenshot.Memento { } public void Dispose() { - if (drawableContainer != null) { - drawableContainer.Dispose(); - drawableContainer = null; + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (drawableContainer != null) { + drawableContainer.Dispose(); + drawableContainer = null; + } } } diff --git a/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs b/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs index 2240a6f40..69937853b 100644 --- a/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs +++ b/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs @@ -54,6 +54,13 @@ namespace Greenshot.Memento { } public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + // if (disposing) { } + listOfdrawableContainer = null; } public LangKey ActionLanguageKey { diff --git a/Greenshot/Memento/SurfaceBackgroundChangeMemento.cs b/Greenshot/Memento/SurfaceBackgroundChangeMemento.cs index 191c2444c..2f6db16f5 100644 --- a/Greenshot/Memento/SurfaceBackgroundChangeMemento.cs +++ b/Greenshot/Memento/SurfaceBackgroundChangeMemento.cs @@ -40,9 +40,17 @@ namespace Greenshot.Memento { } public void Dispose() { - if (image != null) { - image.Dispose(); - image = null; + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (image != null) { + image.Dispose(); + image = null; + } + surface = null; } } diff --git a/Greenshot/Memento/TextChangeMemento.cs b/Greenshot/Memento/TextChangeMemento.cs index 81d0372ab..dbca4b07b 100644 --- a/Greenshot/Memento/TextChangeMemento.cs +++ b/Greenshot/Memento/TextChangeMemento.cs @@ -36,6 +36,14 @@ namespace Greenshot.Memento { } public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + textContainer = null; + } } public LangKey ActionLanguageKey { diff --git a/GreenshotBoxPlugin/BoxPlugin.cs b/GreenshotBoxPlugin/BoxPlugin.cs index 1a937e1bf..0f61db4a3 100644 --- a/GreenshotBoxPlugin/BoxPlugin.cs +++ b/GreenshotBoxPlugin/BoxPlugin.cs @@ -41,6 +41,20 @@ namespace GreenshotBoxPlugin { private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInConfig; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInConfig != null) { + itemPlugInConfig.Dispose(); + itemPlugInConfig = null; + } + } + } + public BoxPlugin() { } @@ -76,7 +90,7 @@ namespace GreenshotBoxPlugin { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("box", LangKey.Configure); } diff --git a/GreenshotConfluencePlugin/Confluence.cs b/GreenshotConfluencePlugin/Confluence.cs index be806a406..018631234 100644 --- a/GreenshotConfluencePlugin/Confluence.cs +++ b/GreenshotConfluencePlugin/Confluence.cs @@ -91,7 +91,7 @@ namespace Confluence { } #endregion - public class ConfluenceConnector { + public class ConfluenceConnector : IDisposable { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluenceConnector)); private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.confluence.rpc.AuthenticationFailedException"; private const string V2_FAILED = "AXIS"; @@ -104,6 +104,23 @@ namespace Confluence { private string url; private Cache pageCache = new Cache(60 * config.Timeout); + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (confluence != null) { + logout(); + } + if (disposing) { + if (confluence != null) { + confluence.Dispose(); + confluence = null; + } + } + } + public ConfluenceConnector(string url, int timeout) { this.timeout = timeout; init(url); @@ -117,7 +134,7 @@ namespace Confluence { } ~ConfluenceConnector() { - logout(); + Dispose(false); } /// diff --git a/GreenshotConfluencePlugin/ConfluencePlugin.cs b/GreenshotConfluencePlugin/ConfluencePlugin.cs index 5ab80f3f8..dec01683e 100644 --- a/GreenshotConfluencePlugin/ConfluencePlugin.cs +++ b/GreenshotConfluencePlugin/ConfluencePlugin.cs @@ -39,6 +39,15 @@ namespace GreenshotConfluencePlugin { private static ConfluenceConfiguration config = null; private static IGreenshotHost host; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) {} + } + private static void CreateConfluenceConntector() { if (confluenceConnector == null) { if (config.Url.Contains("soap-axis")) { diff --git a/GreenshotConfluencePlugin/Forms/ConfluenceSearch.xaml.cs b/GreenshotConfluencePlugin/Forms/ConfluenceSearch.xaml.cs index b8a3a1279..85905adb5 100644 --- a/GreenshotConfluencePlugin/Forms/ConfluenceSearch.xaml.cs +++ b/GreenshotConfluencePlugin/Forms/ConfluenceSearch.xaml.cs @@ -31,7 +31,6 @@ namespace GreenshotConfluencePlugin { public partial class ConfluenceSearch : System.Windows.Controls.Page { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluenceSearch)); private static ConfluenceConfiguration config = IniConfig.GetIniSection(); - private ConfluenceConnector confluenceConnector; private ConfluenceUpload confluenceUpload; public List Spaces { @@ -48,7 +47,6 @@ namespace GreenshotConfluencePlugin { } public ConfluenceSearch(ConfluenceUpload confluenceUpload) { - this.confluenceConnector = ConfluencePlugin.ConfluenceConnector; this.confluenceUpload = confluenceUpload; this.DataContext = this; InitializeComponent(); @@ -81,7 +79,7 @@ namespace GreenshotConfluencePlugin { void doSearch() { string spaceKey = (string)SpaceComboBox.SelectedValue; config.SearchSpaceKey = spaceKey; - List searchResult = confluenceConnector.searchPages(searchText.Text, spaceKey); + List searchResult = ConfluencePlugin.ConfluenceConnector.searchPages(searchText.Text, spaceKey); pages.Clear(); foreach(Confluence.Page page in searchResult) { pages.Add(page); diff --git a/GreenshotDropboxPlugin/DropboxPlugin.cs b/GreenshotDropboxPlugin/DropboxPlugin.cs index 07cdd3fe6..1cd482464 100644 --- a/GreenshotDropboxPlugin/DropboxPlugin.cs +++ b/GreenshotDropboxPlugin/DropboxPlugin.cs @@ -41,6 +41,20 @@ namespace GreenshotDropboxPlugin { private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInConfig; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInConfig != null) { + itemPlugInConfig.Dispose(); + itemPlugInConfig = null; + } + } + } + public DropboxPlugin() { } @@ -77,7 +91,7 @@ namespace GreenshotDropboxPlugin { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("dropbox", LangKey.Configure); } diff --git a/GreenshotDropboxPlugin/DropboxUtils.cs b/GreenshotDropboxPlugin/DropboxUtils.cs index 06035b576..cec342cfd 100644 --- a/GreenshotDropboxPlugin/DropboxUtils.cs +++ b/GreenshotDropboxPlugin/DropboxUtils.cs @@ -53,7 +53,7 @@ namespace GreenshotDropboxPlugin { LOG.DebugFormat("Upload response: {0}", uploadResponse); } catch (Exception ex) { LOG.Error("Upload error: ", ex); - throw ex; + throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.DropboxToken = oAuth.Token; diff --git a/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs b/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs index 6a0f55057..830fb54cf 100644 --- a/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs +++ b/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs @@ -37,6 +37,20 @@ namespace ExternalCommand { private PluginAttribute myAttributes; private ToolStripMenuItem itemPlugInRoot; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInRoot != null) { + itemPlugInRoot.Dispose(); + itemPlugInRoot = null; + } + } + } + public ExternalCommandPlugin() { } @@ -83,7 +97,7 @@ namespace ExternalCommand { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInRoot != null) { itemPlugInRoot.Text = Language.GetString("externalcommand", "contextmenu_configure"); } diff --git a/GreenshotFlickrPlugin/FlickrPlugin.cs b/GreenshotFlickrPlugin/FlickrPlugin.cs index 27894e5b0..740105b5d 100644 --- a/GreenshotFlickrPlugin/FlickrPlugin.cs +++ b/GreenshotFlickrPlugin/FlickrPlugin.cs @@ -42,6 +42,19 @@ namespace GreenshotFlickrPlugin private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInConfig; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInConfig != null) { + itemPlugInConfig.Dispose(); + itemPlugInConfig = null; + } + } + } public FlickrPlugin() { } @@ -79,7 +92,7 @@ namespace GreenshotFlickrPlugin return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); } diff --git a/GreenshotFlickrPlugin/FlickrUtils.cs b/GreenshotFlickrPlugin/FlickrUtils.cs index 1e9ecbc5b..9df5c0384 100644 --- a/GreenshotFlickrPlugin/FlickrUtils.cs +++ b/GreenshotFlickrPlugin/FlickrUtils.cs @@ -87,7 +87,7 @@ namespace GreenshotFlickrPlugin { return GetUrl(photoInfo); } catch (Exception ex) { LOG.Error("Upload error: ", ex); - throw ex; + throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.FlickrToken = oAuth.Token; diff --git a/GreenshotImgurPlugin/ImgurPlugin.cs b/GreenshotImgurPlugin/ImgurPlugin.cs index 8a760310c..e07878c12 100644 --- a/GreenshotImgurPlugin/ImgurPlugin.cs +++ b/GreenshotImgurPlugin/ImgurPlugin.cs @@ -43,6 +43,24 @@ namespace GreenshotImgurPlugin { private ToolStripMenuItem historyMenuItem = null; private ToolStripMenuItem itemPlugInConfig; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (historyMenuItem != null) { + historyMenuItem.Dispose(); + historyMenuItem = null; + } + if (itemPlugInConfig != null) { + itemPlugInConfig.Dispose(); + itemPlugInConfig = null; + } + } + } + public ImgurPlugin() { } @@ -98,7 +116,7 @@ namespace GreenshotImgurPlugin { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("imgur", LangKey.configure); } diff --git a/GreenshotImgurPlugin/ImgurUtils.cs b/GreenshotImgurPlugin/ImgurUtils.cs index 362094efa..e90278128 100644 --- a/GreenshotImgurPlugin/ImgurUtils.cs +++ b/GreenshotImgurPlugin/ImgurUtils.cs @@ -131,7 +131,7 @@ namespace GreenshotImgurPlugin { } } catch (Exception ex) { LOG.Error("Upload to imgur gave an exeption: ", ex); - throw ex; + throw; } } else { OAuthSession oAuth = new OAuthSession(ImgurCredentials.CONSUMER_KEY, ImgurCredentials.CONSUMER_SECRET); @@ -160,7 +160,7 @@ namespace GreenshotImgurPlugin { responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.imgur.com/2/account/images.xml", uploadParameters, otherParameters, null); } catch (Exception ex) { LOG.Error("Upload to imgur gave an exeption: ", ex); - throw ex; + throw; } finally { if (oAuth.Token != null) { config.ImgurToken = oAuth.Token; @@ -212,7 +212,7 @@ namespace GreenshotImgurPlugin { return null; } } - throw wE; + throw; } LOG.Debug(responseString); ImgurInfo imgurInfo = ImgurInfo.ParseResponse(responseString); @@ -243,7 +243,7 @@ namespace GreenshotImgurPlugin { // Allow "Bad request" this means we already deleted it if (wE.Status == WebExceptionStatus.ProtocolError) { if (((HttpWebResponse)wE.Response).StatusCode != HttpStatusCode.BadRequest) { - throw wE; + throw ; } } } diff --git a/GreenshotJiraPlugin/Jira.cs b/GreenshotJiraPlugin/Jira.cs index c6f5ed6e6..c0be9dbea 100644 --- a/GreenshotJiraPlugin/Jira.cs +++ b/GreenshotJiraPlugin/Jira.cs @@ -95,7 +95,7 @@ namespace Jira { } #endregion - public class JiraConnector { + public class JiraConnector : IDisposable { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraConnector)); private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.jira.rpc.exception.RemoteAuthenticationException"; private static JiraConfiguration config = IniConfig.GetIniSection(); @@ -110,6 +110,24 @@ namespace Jira { private Cache userCache = new Cache(60 * config.Timeout); private bool suppressBackgroundForm = false; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (jira != null) { + logout(); + } + + if (disposing) { + if (jira != null) { + jira.Dispose(); + jira = null; + } + } + } + public JiraConnector() : this(false) { } @@ -138,7 +156,7 @@ namespace Jira { } ~JiraConnector() { - logout(); + Dispose(false); } /// @@ -152,7 +170,7 @@ namespace Jira { LOG.DebugFormat("Loggin in"); try { this.credentials = jira.login(user, password); - } catch(Exception ex) { + } catch (Exception) { if (!url.EndsWith("wsdl")) { url = url + "/rpc/soap/jirasoapservice-v2?wsdl"; // recreate the service with the new url @@ -162,7 +180,7 @@ namespace Jira { config.Url = url; IniConfig.Save(); } else { - throw ex; + throw; } } @@ -324,7 +342,7 @@ namespace Jira { jira.addAttachmentsToIssue(credentials, issueKey, new string[] { filename }, (sbyte[])(Array)attachment.ToByteArray()); } catch (Exception ex2) { LOG.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message); - throw ex2; + throw; } } } diff --git a/GreenshotJiraPlugin/JiraPlugin.cs b/GreenshotJiraPlugin/JiraPlugin.cs index dc80a740a..8f4b6b409 100644 --- a/GreenshotJiraPlugin/JiraPlugin.cs +++ b/GreenshotJiraPlugin/JiraPlugin.cs @@ -24,6 +24,7 @@ using System.Windows.Forms; using Greenshot.IniFile; using Greenshot.Plugin; using Jira; +using System; namespace GreenshotJiraPlugin { /// @@ -37,7 +38,21 @@ namespace GreenshotJiraPlugin { private JiraConfiguration config = null; private ComponentResourceManager resources; private static JiraPlugin instance = null; - + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (jiraConnector != null) { + jiraConnector.Dispose(); + jiraConnector = null; + } + } + } + public static JiraPlugin Instance { get { return instance; diff --git a/GreenshotOCRCommand/COMWrapper.cs b/GreenshotOCRCommand/COMWrapper.cs index 458308593..5c7fd3298 100644 --- a/GreenshotOCRCommand/COMWrapper.cs +++ b/GreenshotOCRCommand/COMWrapper.cs @@ -228,7 +228,6 @@ namespace Greenshot.Interop { /// sure that the COM object is still cleaned up. /// ~COMWrapper() { - //LOG.DebugFormat("Finalize {0}", this._InterceptType.ToString()); this.Dispose(false); } @@ -248,12 +247,10 @@ namespace Greenshot.Interop { /// interface. /// private void Dispose(bool disposing) { - if (null != this._COMObject) { - //LOG.DebugFormat("Disposing {0}", this._InterceptType.ToString()); + if (disposing && null != this._COMObject) { if (Marshal.IsComObject(this._COMObject)) { try { - while (Marshal.ReleaseComObject(this._COMObject) > 0) - ; + while (Marshal.ReleaseComObject(this._COMObject) > 0); } catch (Exception) { //LOG.WarnFormat("Problem releasing {0}", _COMType); //LOG.Warn("Error: ", ex); @@ -638,10 +635,10 @@ namespace Greenshot.Interop { /// public string TypeName { get { - throw new NotImplementedException(); + throw new NotSupportedException(); } set { - throw new NotImplementedException(); + throw new NotSupportedException(); } } } diff --git a/GreenshotOCRPlugin/OCRPlugin.cs b/GreenshotOCRPlugin/OCRPlugin.cs index f9e3d2b8d..093040bde 100644 --- a/GreenshotOCRPlugin/OCRPlugin.cs +++ b/GreenshotOCRPlugin/OCRPlugin.cs @@ -69,6 +69,20 @@ namespace GreenshotOCR { private PluginAttribute myAttributes; private ToolStripMenuItem ocrMenuItem = new ToolStripMenuItem(); + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (ocrMenuItem != null) { + ocrMenuItem.Dispose(); + ocrMenuItem = null; + } + } + } + public OcrPlugin() { } public IEnumerable Destinations() { diff --git a/GreenshotOfficePlugin/OfficeExport/OneNoteExporter.cs b/GreenshotOfficePlugin/OfficeExport/OneNoteExporter.cs index 034786f7a..bd3dbd22b 100644 --- a/GreenshotOfficePlugin/OfficeExport/OneNoteExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/OneNoteExporter.cs @@ -70,8 +70,11 @@ namespace Greenshot.Interop.Office { oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { LOG.Debug(notebookXml); - using (StringReader reader = new StringReader(notebookXml)) { + StringReader reader = null; + try { + reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { + reader = null; while (xmlReader.Read()) { if ("one:Page".Equals(xmlReader.Name)) { if ("true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"))) { @@ -87,6 +90,10 @@ namespace Greenshot.Interop.Office { } } } + } finally { + if (reader != null) { + reader.Dispose(); + } } } } diff --git a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs index c9c28b69b..b036b05dd 100644 --- a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs @@ -348,7 +348,7 @@ namespace Greenshot.Interop.Office { href = string.Format("", url); hrefEnd = ""; } - string htmlImgEmbedded = string.Format("
{0}\"{1}\"
", href, attachmentName, contentID, hrefEnd); + string htmlImgEmbedded = string.Format("
{0}\"{1}\"{3}
", href, attachmentName, contentID, hrefEnd); string fallbackBody = string.Format("{0}", htmlImgEmbedded); if (bodyString == null) { bodyString = fallbackBody; diff --git a/GreenshotOfficePlugin/OfficeInterop/OutlookUtils.cs b/GreenshotOfficePlugin/OfficeInterop/OutlookUtils.cs index 9c7f9aa44..ab454e4b6 100644 --- a/GreenshotOfficePlugin/OfficeInterop/OutlookUtils.cs +++ b/GreenshotOfficePlugin/OfficeInterop/OutlookUtils.cs @@ -606,56 +606,6 @@ namespace Greenshot.Interop.Office { public long filler; } - /// - /// Use MAPI32.DLL "HrGetOneProp" from managed code - /// - /// - /// - /// - public static string GetMAPIProperty(Attachment attachment, PropTags proptag) { - object mapiObject = attachment.MAPIOBJECT; - if (mapiObject == null) { - return ""; - } - - string sProperty = ""; - IntPtr pPropValue = IntPtr.Zero; - - IntPtr IUnknown = IntPtr.Zero; - IntPtr IMAPIProperty = IntPtr.Zero; - - try { - MAPIInitialize(IntPtr.Zero); - IUnknown = Marshal.GetIUnknownForObject(mapiObject); - Guid guidMAPIProp = new Guid(IID_IMAPIProp); - if (Marshal.QueryInterface(IUnknown, ref guidMAPIProp, out IMAPIProperty) != 0) { - return ""; - } - try { - HrGetOneProp(IMAPIProperty, (uint)proptag, out pPropValue); - if (pPropValue == IntPtr.Zero) { - return ""; - } - SPropValue propValue = (SPropValue)Marshal.PtrToStructure(pPropValue, typeof(SPropValue)); - sProperty = Marshal.PtrToStringUni(propValue.Value); - } catch (System.Exception ex) { - throw ex; - } - } finally { - if (pPropValue != IntPtr.Zero) { - MAPIFreeBuffer(pPropValue); - } - if (IMAPIProperty != IntPtr.Zero) { - Marshal.Release(IMAPIProperty); - } - if (IUnknown != IntPtr.Zero) { - Marshal.Release(IUnknown); - } - MAPIUninitialize(); - } - return sProperty; - } - /// /// Tries to save the changes we just made /// diff --git a/GreenshotOfficePlugin/OfficePlugin.cs b/GreenshotOfficePlugin/OfficePlugin.cs index 2c72b63ed..c76ad1650 100644 --- a/GreenshotOfficePlugin/OfficePlugin.cs +++ b/GreenshotOfficePlugin/OfficePlugin.cs @@ -18,6 +18,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +using System; using System.Collections.Generic; using System.Windows.Forms; @@ -32,6 +33,15 @@ namespace GreenshotOfficePlugin { public static PluginAttribute Attributes; private IGreenshotHost host; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) {} + } + public OfficePlugin() { } diff --git a/GreenshotPhotobucketPlugin/PhotobucketPlugin.cs b/GreenshotPhotobucketPlugin/PhotobucketPlugin.cs index 6ac606a2b..b806c3be5 100644 --- a/GreenshotPhotobucketPlugin/PhotobucketPlugin.cs +++ b/GreenshotPhotobucketPlugin/PhotobucketPlugin.cs @@ -41,6 +41,20 @@ namespace GreenshotPhotobucketPlugin { private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInConfig; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInConfig != null) { + itemPlugInConfig.Dispose(); + itemPlugInConfig = null; + } + } + } + public PhotobucketPlugin() { } @@ -79,7 +93,7 @@ namespace GreenshotPhotobucketPlugin { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("photobucket", LangKey.configure); } diff --git a/GreenshotPhotobucketPlugin/PhotobucketUtils.cs b/GreenshotPhotobucketPlugin/PhotobucketUtils.cs index f85591350..8d1602c7d 100644 --- a/GreenshotPhotobucketPlugin/PhotobucketUtils.cs +++ b/GreenshotPhotobucketPlugin/PhotobucketUtils.cs @@ -74,7 +74,7 @@ namespace GreenshotPhotobucketPlugin { responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null); } catch (Exception ex) { LOG.Error("Error uploading to Photobucket.", ex); - throw ex; + throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.Token = oAuth.Token; @@ -148,7 +148,7 @@ namespace GreenshotPhotobucketPlugin { responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, null, null); } catch (Exception ex) { LOG.Error("Error uploading to Photobucket.", ex); - throw ex; + throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.Token = oAuth.Token; diff --git a/GreenshotPicasaPlugin/PicasaPlugin.cs b/GreenshotPicasaPlugin/PicasaPlugin.cs index cb2b2eef5..c81526b16 100644 --- a/GreenshotPicasaPlugin/PicasaPlugin.cs +++ b/GreenshotPicasaPlugin/PicasaPlugin.cs @@ -40,6 +40,20 @@ namespace GreenshotPicasaPlugin { private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInRoot; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (itemPlugInRoot != null) { + itemPlugInRoot.Dispose(); + itemPlugInRoot = null; + } + } + } + public PicasaPlugin() { } @@ -76,7 +90,7 @@ namespace GreenshotPicasaPlugin { return true; } - public void OnLanguageChanged() { + public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInRoot != null) { itemPlugInRoot.Text = Language.GetString("picasa", LangKey.Configure); } diff --git a/GreenshotPicasaPlugin/PicasaUtils.cs b/GreenshotPicasaPlugin/PicasaUtils.cs index 255d1aba7..e5316ca0d 100644 --- a/GreenshotPicasaPlugin/PicasaUtils.cs +++ b/GreenshotPicasaPlugin/PicasaUtils.cs @@ -71,7 +71,7 @@ namespace GreenshotPicasaPlugin { return ParseResponse(response); } catch (Exception ex) { LOG.Error("Upload error: ", ex); - throw ex; + throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.PicasaToken = oAuth.Token; diff --git a/GreenshotPlugin/Controls/PleaseWaitForm.cs b/GreenshotPlugin/Controls/PleaseWaitForm.cs index bcaa8a0e5..eaadb9d30 100644 --- a/GreenshotPlugin/Controls/PleaseWaitForm.cs +++ b/GreenshotPlugin/Controls/PleaseWaitForm.cs @@ -94,7 +94,7 @@ namespace GreenshotPlugin.Controls { LOG.DebugFormat("Finished {0}", title); } catch (Exception ex) { LOG.Error(ex); - throw ex; + throw; } finally { Close(); } diff --git a/GreenshotPlugin/Controls/SaveImageFileDialog.cs b/GreenshotPlugin/Controls/SaveImageFileDialog.cs index dead94596..fb6232adf 100644 --- a/GreenshotPlugin/Controls/SaveImageFileDialog.cs +++ b/GreenshotPlugin/Controls/SaveImageFileDialog.cs @@ -31,7 +31,7 @@ namespace GreenshotPlugin.Controls { /// Custom dialog for saving images, wraps SaveFileDialog. /// For some reason SFD is sealed :( ///
- public class SaveImageFileDialog { + public class SaveImageFileDialog : IDisposable { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SaveImageFileDialog)); private static CoreConfiguration conf = IniConfig.GetIniSection(); protected SaveFileDialog saveFileDialog; @@ -39,6 +39,20 @@ namespace GreenshotPlugin.Controls { private DirectoryInfo eagerlyCreatedDirectory; private ICaptureDetails captureDetails = null; + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (saveFileDialog != null) { + saveFileDialog.Dispose(); + saveFileDialog = null; + } + } + } + public SaveImageFileDialog() { init(); } diff --git a/GreenshotPlugin/Core/AbstractDestination.cs b/GreenshotPlugin/Core/AbstractDestination.cs index 570e93f81..ed53c7c08 100644 --- a/GreenshotPlugin/Core/AbstractDestination.cs +++ b/GreenshotPlugin/Core/AbstractDestination.cs @@ -136,8 +136,14 @@ namespace GreenshotPlugin.Core { public virtual IEnumerable DynamicDestinations() { yield break; } - - public virtual void Dispose() { + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) {} } public virtual bool isDynamic { diff --git a/GreenshotPlugin/Core/AbstractProcessor.cs b/GreenshotPlugin/Core/AbstractProcessor.cs index 6c5ab475d..c0efbe798 100644 --- a/GreenshotPlugin/Core/AbstractProcessor.cs +++ b/GreenshotPlugin/Core/AbstractProcessor.cs @@ -18,7 +18,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - +using System; using Greenshot.Plugin; @@ -52,8 +52,14 @@ namespace GreenshotPlugin.Core { return 10; } } - - public virtual void Dispose() { + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + //if (disposing) {} } public virtual bool isActive { diff --git a/GreenshotPlugin/Core/AccessibleHelper.cs b/GreenshotPlugin/Core/AccessibleHelper.cs index fc4e438f2..fe550e974 100644 --- a/GreenshotPlugin/Core/AccessibleHelper.cs +++ b/GreenshotPlugin/Core/AccessibleHelper.cs @@ -43,9 +43,9 @@ namespace GreenshotPlugin.Core { return num; } [DllImport("oleacc.dll")] - public static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint id, ref Guid iid, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject); + private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint id, ref Guid iid, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject); [DllImport("oleacc.dll")] - public static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgvarChildren, out int pcObtained); + private static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgvarChildren, out int pcObtained); [DllImport("oleacc.dll", PreserveSig=false)] [return: MarshalAs(UnmanagedType.Interface)] diff --git a/GreenshotPlugin/Core/FilenameHelper.cs b/GreenshotPlugin/Core/FilenameHelper.cs index e66948eac..bc88e1168 100644 --- a/GreenshotPlugin/Core/FilenameHelper.cs +++ b/GreenshotPlugin/Core/FilenameHelper.cs @@ -435,7 +435,7 @@ namespace GreenshotPlugin.Core { // adding additional data for bug tracking e.Data.Add("title", captureDetails.Title); e.Data.Add("pattern", pattern); - throw e; + throw; } } } diff --git a/GreenshotPlugin/Core/ImageOutput.cs b/GreenshotPlugin/Core/ImageOutput.cs index fd38a6b24..8bf639032 100644 --- a/GreenshotPlugin/Core/ImageOutput.cs +++ b/GreenshotPlugin/Core/ImageOutput.cs @@ -406,22 +406,23 @@ namespace GreenshotPlugin.Core { /// Path to filename public static string SaveWithDialog(ISurface surface, ICaptureDetails captureDetails) { string returnValue = null; - SaveImageFileDialog saveImageFileDialog = new SaveImageFileDialog(captureDetails); - DialogResult dialogResult = saveImageFileDialog.ShowDialog(); - if (dialogResult.Equals(DialogResult.OK)) { - try { - string fileNameWithExtension = saveImageFileDialog.FileNameWithExtension; - SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(FormatForFilename(fileNameWithExtension)); - if (conf.OutputFilePromptQuality) { - QualityDialog qualityDialog = new QualityDialog(outputSettings); - qualityDialog.ShowDialog(); + using (SaveImageFileDialog saveImageFileDialog = new SaveImageFileDialog(captureDetails)) { + DialogResult dialogResult = saveImageFileDialog.ShowDialog(); + if (dialogResult.Equals(DialogResult.OK)) { + try { + string fileNameWithExtension = saveImageFileDialog.FileNameWithExtension; + SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(FormatForFilename(fileNameWithExtension)); + if (conf.OutputFilePromptQuality) { + QualityDialog qualityDialog = new QualityDialog(outputSettings); + qualityDialog.ShowDialog(); + } + // TODO: For now we always overwrite, should be changed + ImageOutput.Save(surface, fileNameWithExtension, true, outputSettings, conf.OutputFileCopyPathToClipboard); + returnValue = fileNameWithExtension; + IniConfig.Save(); + } catch (System.Runtime.InteropServices.ExternalException) { + MessageBox.Show(Language.GetFormattedString("error_nowriteaccess", saveImageFileDialog.FileName).Replace(@"\\", @"\"), Language.GetString("error")); } - // TODO: For now we always overwrite, should be changed - ImageOutput.Save(surface, fileNameWithExtension, true, outputSettings, conf.OutputFileCopyPathToClipboard); - returnValue = fileNameWithExtension; - IniConfig.Save(); - } catch (System.Runtime.InteropServices.ExternalException) { - MessageBox.Show(Language.GetFormattedString("error_nowriteaccess", saveImageFileDialog.FileName).Replace(@"\\", @"\"), Language.GetString("error")); } } return returnValue; diff --git a/GreenshotPlugin/Core/Language.cs b/GreenshotPlugin/Core/Language.cs index 4eb6fd67b..1bd68f45a 100644 --- a/GreenshotPlugin/Core/Language.cs +++ b/GreenshotPlugin/Core/Language.cs @@ -28,7 +28,7 @@ using Greenshot.IniFile; using Microsoft.Win32; namespace GreenshotPlugin.Core { - public delegate void LanguageChangedHandler(); + public delegate void LanguageChangedHandler(object sender, EventArgs e); /// /// This class supplies the GUI with translations, based upon keys. /// The language resources are loaded from the language files found on fixed or supplied paths @@ -202,7 +202,7 @@ namespace GreenshotPlugin.Core { Reload(); if (LanguageChanged != null) { try { - LanguageChanged(); + LanguageChanged(null, null); } catch { } } diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index cfe1fee5e..b7efdc0c6 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -309,7 +309,7 @@ namespace GreenshotPlugin.Core { using (Stream responseStream = response.GetResponseStream()) { LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, new StreamReader(responseStream, true).ReadToEnd()); } - throw e; + throw; } return responseData; diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index d26557bcb..e8ca8a930 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -405,7 +405,7 @@ namespace GreenshotPlugin.Core { return getAccessToken() != null; } catch (Exception ex) { LOG.Error(ex); - throw ex; + throw; } } @@ -518,7 +518,7 @@ namespace GreenshotPlugin.Core { continue; } } - throw wEx; + throw; } } if (lastException != null) { diff --git a/GreenshotPlugin/Core/QuantizerHelper.cs b/GreenshotPlugin/Core/QuantizerHelper.cs index 313388bc7..45219833f 100644 --- a/GreenshotPlugin/Core/QuantizerHelper.cs +++ b/GreenshotPlugin/Core/QuantizerHelper.cs @@ -101,9 +101,16 @@ namespace GreenshotPlugin.Core { private Bitmap resultBitmap; public void Dispose() { - if (resultBitmap != null) { - resultBitmap.Dispose(); - resultBitmap = null; + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) { + if (disposing) { + if (resultBitmap != null) { + resultBitmap.Dispose(); + resultBitmap = null; + } } } diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index d3a81754b..8a07504d8 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -196,7 +196,6 @@ namespace GreenshotPlugin.Core { private WindowDetails parent = null; private bool frozen = false; - public bool isApp { get { return METRO_WINDOWS_CLASS.Equals(ClassName); @@ -1105,6 +1104,7 @@ namespace GreenshotPlugin.Core { tempForm.Close(); } tempForm.Dispose(); + tempForm = null; } } diff --git a/GreenshotPlugin/IniFile/IniValue.cs b/GreenshotPlugin/IniFile/IniValue.cs index b0393e531..6fc34a60f 100644 --- a/GreenshotPlugin/IniFile/IniValue.cs +++ b/GreenshotPlugin/IniFile/IniValue.cs @@ -372,7 +372,7 @@ namespace Greenshot.IniFile { if (valueString.Length > 0) { try { return Enum.Parse(valueType, valueString); - } catch (ArgumentException ae) { + } catch (ArgumentException) { //LOG.InfoFormat("Couldn't match {0} to {1}, trying case-insentive match", valueString, fieldType); foreach (Enum enumValue in Enum.GetValues(valueType)) { if (enumValue.ToString().Equals(valueString, StringComparison.InvariantCultureIgnoreCase)) { @@ -380,7 +380,7 @@ namespace Greenshot.IniFile { return enumValue; } } - throw ae; + throw; } } } diff --git a/GreenshotPlugin/Interfaces/Drawing/Container.cs b/GreenshotPlugin/Interfaces/Drawing/Container.cs index 6857cf529..b48b6466f 100644 --- a/GreenshotPlugin/Interfaces/Drawing/Container.cs +++ b/GreenshotPlugin/Interfaces/Drawing/Container.cs @@ -28,7 +28,7 @@ namespace Greenshot.Plugin.Drawing { public enum RenderMode {EDIT, EXPORT}; public enum EditStatus {UNDRAWN, DRAWING, MOVING, RESIZING, IDLE}; - public interface IDrawableContainer : INotifyPropertyChanged { + public interface IDrawableContainer : INotifyPropertyChanged, IDisposable { ISurface Parent { get; } @@ -83,7 +83,6 @@ namespace Greenshot.Plugin.Drawing { } void AlignToParent(HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment); void Invalidate(); - void Dispose(); bool ClickableAt(int x, int y); void HideGrippers(); void ShowGrippers(); diff --git a/GreenshotPlugin/Interfaces/Generic.cs b/GreenshotPlugin/Interfaces/Generic.cs index 27a518d48..6e1e9d577 100644 --- a/GreenshotPlugin/Interfaces/Generic.cs +++ b/GreenshotPlugin/Interfaces/Generic.cs @@ -56,11 +56,25 @@ namespace Greenshot.Plugin { set; } } + + public class SurfaceElementEventArgs : EventArgs { + public IList Elements { + get; + set; + } + } + + public class SurfaceDrawingModeEventArgs : EventArgs { + public DrawingModes DrawingMode { + get; + set; + } + } - public delegate void SurfaceSizeChangeEventHandler(object source); - public delegate void SurfaceMessageEventHandler(object source, SurfaceMessageEventArgs eventArgs); - public delegate void SurfaceElementEventHandler(object source, IList element); - public delegate void SurfaceDrawingModeEventHandler(object source, DrawingModes drawingMode); + public delegate void SurfaceSizeChangeEventHandler(object sender, EventArgs eventArgs); + public delegate void SurfaceMessageEventHandler(object sender, SurfaceMessageEventArgs eventArgs); + public delegate void SurfaceElementEventHandler(object sender, SurfaceElementEventArgs eventArgs); + public delegate void SurfaceDrawingModeEventHandler(object sender, SurfaceDrawingModeEventArgs eventArgs); public enum DrawingModes { None, Rect, Ellipse, Text, Line, Arrow, Crop, Highlight, Obfuscate, Bitmap, Path } /// diff --git a/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs b/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs index cfe3ed23f..3229cac69 100644 --- a/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs +++ b/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs @@ -225,7 +225,7 @@ namespace Greenshot.Plugin { ICapture GetCapture(Image imageToCapture); } - public interface IGreenshotPlugin { + public interface IGreenshotPlugin : IDisposable { /// /// Is called after the plugin is instanciated, the Plugin should keep a copy of the host and pluginAttribute. /// diff --git a/GreenshotPlugin/Interop/COMWrapper.cs b/GreenshotPlugin/Interop/COMWrapper.cs index b4752fd7b..2545ef272 100644 --- a/GreenshotPlugin/Interop/COMWrapper.cs +++ b/GreenshotPlugin/Interop/COMWrapper.cs @@ -764,10 +764,10 @@ namespace Greenshot.Interop { /// public string TypeName { get { - throw new NotImplementedException(); + throw new NotSupportedException(); } set { - throw new NotImplementedException(); + throw new NotSupportedException(); } } } diff --git a/GreenshotPlugin/UnmanagedHelpers/User32.cs b/GreenshotPlugin/UnmanagedHelpers/User32.cs index 577657ff1..ce1ef4b90 100644 --- a/GreenshotPlugin/UnmanagedHelpers/User32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/User32.cs @@ -27,6 +27,7 @@ using System.Text; using Microsoft.Win32.SafeHandles; using System.Security; +using System.Security.Permissions; namespace GreenshotPlugin.UnmanagedHelpers { /// @@ -297,6 +298,7 @@ namespace GreenshotPlugin.UnmanagedHelpers { this.SetHandle(hIcon); } + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] protected override bool ReleaseHandle() { return User32.DestroyIcon(this.handle); } @@ -322,6 +324,7 @@ namespace GreenshotPlugin.UnmanagedHelpers { SetHandle(preexistingHandle); } + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)] protected override bool ReleaseHandle() { bool returnValue = ReleaseDC(hWnd, handle); return returnValue;