Skip to content

Commit

Permalink
Addressed minor problems reported by Coverity Scan
Browse files Browse the repository at this point in the history
Fixed many minor resource leaks. These Windows handles and GDI handles
were not being released as soon as they should have been, but instead
had to wait for the garbage collector. The highest frequency leaks were
System.Drawing.Drawing2D.Matrix objects, but diagnostic tools, like
Process Explorer, did not actually measure any increase in GDI handles
as the leaks were occurring.

ObservableDomNodeAdapter: fixed a lock problem that was designed to
allow for safe multiple thread access.

Removed some useless code in KeysInterop.ToWpfModifiers(). These two
lines could never do anything because AtfKeys.RWin is not a flag in
AtfKeys.Modifiers, so WpfModifierKeys.Windows could never be added to
the result. This code also tested for AtfKeys.RWin twice. Maybe it was
supposed to test for AtfKeys.LWin? But LWin is not in Modifiers either.
  • Loading branch information
Ron2 committed Sep 3, 2015
1 parent d8e9f73 commit bc26641
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 208 deletions.
10 changes: 6 additions & 4 deletions Framework/Atf.Core/Dom/ObservableDomNodeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public IObservableCollection<T> GetObservableChildList<T>(ChildInfo childInfo)
{
IObservableCollection list;

if (m_childListsCache == null)
m_childListsCache = new Dictionary<ChildInfo, Dictionary<Type, IObservableCollection>>();

lock (m_childListsCache)
// The data-binding might be done on a separate thread than the main UI thread.
lock (m_lock)
{
if (m_childListsCache == null)
m_childListsCache = new Dictionary<ChildInfo, Dictionary<Type, IObservableCollection>>();

Dictionary<Type, IObservableCollection> typeLookup;
if (!m_childListsCache.TryGetValue(childInfo, out typeLookup))
{
Expand Down Expand Up @@ -269,6 +270,7 @@ protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
}

private static readonly Multimap<Type, DomNodeType> s_registeredTypes = new Multimap<Type, DomNodeType>();
private readonly object m_lock = new object(); //for accessing m_childListCache
private Dictionary<ChildInfo, Dictionary<Type, IObservableCollection>> m_childListsCache;
private bool m_hasTags;
private event PropertyChangedEventHandler m_propertyChanged;
Expand Down
5 changes: 4 additions & 1 deletion Framework/Atf.Gui.OpenGL/OpenGlCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public static void InitOpenGl(IntPtr hdc, out IntPtr hglrc)
}

// Init fonts
Gdi.SelectObject(hdc, SystemFonts.DefaultFont.ToHfont());
using (Font defaultFont = SystemFonts.DefaultFont)
{
Gdi.SelectObject(hdc, defaultFont.ToHfont());
}
foreach (IntSet.Range range in s_fontMap.Ranges)
{
int baseDisplayListId = range.PreviousItemsCount + TEXT_DISPLAY_LIST_BASE;
Expand Down
6 changes: 4 additions & 2 deletions Framework/Atf.Gui.WinForms/Applications/OscCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ public virtual void DoCommand(object commandTag)
switch (command)
{
case Command.OscInfo:
var dialog = new OscDialog(OscService, m_commandReceiver);
dialog.ShowDialog(m_mainWindow);
using (var dialog = new OscDialog(OscService, m_commandReceiver))
{
dialog.ShowDialog(m_mainWindow);
}
break;
case Command.CopyOscAddressOfPropertyDescriptor:
Clipboard.SetText(m_oscAddressOfPropertyDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SourceControlCommands(
}

/// <summary>
/// Flag bit maps that determine which commands should appear in the "File/Source Conntrol" menu</summary>
/// Flag bit maps that determine which commands should appear in the "File/Source Control" menu</summary>
[Flags]
public enum CommandRegister
{
Expand Down Expand Up @@ -95,7 +95,7 @@ public CheckoutOnEditBehavior CheckoutOnEditBehavior

/// <summary>
/// Whether to refresh the status after saving a document</summary>
/// <remarks>Subversion usualy does not require check-out to edit a checked-in doc,
/// <remarks>Subversion usually does not require check-out to edit a checked-in doc,
/// need to refresh doc status after saving </remarks>
public bool RefreshStatusOnSave { get; set; }

Expand Down Expand Up @@ -500,7 +500,7 @@ protected virtual bool DoRefresh(bool doing)
return false;
}

List<Uri> uris = new List<Uri>();
var uris = new List<Uri>();
foreach (IResource resource in SourceControlContext.Resources)
GetSourceControlledUri(resource, uris);
SourceControlService.RefreshStatus(uris);
Expand All @@ -525,12 +525,12 @@ protected virtual bool DoReconcile(bool doing)
return false;
}

List<Uri> uris = new List<Uri>();
var uris = new List<Uri>();
foreach (IResource resource in SourceControlContext.Resources)
uris.Add(resource.Uri);

List<Uri> modifiled = new List<Uri>();
List<Uri> localNotInDepot = new List<Uri>();
var modifiled = new List<Uri>();
var localNotInDepot = new List<Uri>();

using (new WaitCursor())
{
Expand All @@ -548,10 +548,12 @@ protected virtual bool DoReconcile(bool doing)
}
}

ReconcileForm form = new ReconcileForm(SourceControlService, modifiled, localNotInDepot);
if (m_mainForm != null)
form.Icon = m_mainForm.Icon;
form.ShowDialog();
using (var form = new ReconcileForm(SourceControlService, modifiled, localNotInDepot))
{
if (m_mainForm != null)
form.Icon = m_mainForm.Icon;
form.ShowDialog();
}

return true;
}
Expand Down Expand Up @@ -665,10 +667,12 @@ protected virtual bool DoCheckIn(bool doing)
}
}

CheckInForm form = new CheckInForm(SourceControlService, toCheckIns);
if (m_mainForm != null)
form.Icon = m_mainForm.Icon;
form.ShowDialog(GetDialogOwner());
using (var form = new CheckInForm(SourceControlService, toCheckIns))
{
if (m_mainForm != null)
form.Icon = m_mainForm.Icon;
form.ShowDialog(GetDialogOwner());
}
}

return result;
Expand Down Expand Up @@ -824,7 +828,7 @@ public enum Command
}

/// <summary>
/// Gets current cource control command</summary>
/// Gets current source control command</summary>
protected Command CurrentCommand
{
get { return m_currentCommnd; }
Expand Down
29 changes: 13 additions & 16 deletions Framework/Atf.Gui.WinForms/Controls/Adaptable/GridAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,18 @@ private void control_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (m_visible)
{
Matrix transform = new Matrix();
bool disposeOfTransform = false;
Matrix transform;
if (m_transformAdapter != null)
{
transform = m_transformAdapter.Transform;

}
else
{
transform = new Matrix();
disposeOfTransform = true;
}

RectangleF clientRect = AdaptedControl.ClientRectangle;
RectangleF canvasRect = GdiUtil.InverseTransform(transform, clientRect);

Expand All @@ -202,6 +210,9 @@ private void control_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

// draw vertical lines
ChartUtil.DrawVerticalGrid(transform, canvasRect, m_horizontalGridSpacing, m_gridColor, e.Graphics);

if (disposeOfTransform)
transform.Dispose();
}
}

Expand All @@ -210,26 +221,12 @@ private void control_DrawingD2d(object sender, EventArgs e)
{
if (m_visible)
{
// Matrix transform = new Matrix();
// if (m_transformAdapter != null)
// transform = m_transformAdapter.Transform;

var d2dControl = AdaptedControl as D2dAdaptableControl;

Matrix3x2F xform = d2dControl.D2dGraphics.Transform;
d2dControl.D2dGraphics.Transform = Matrix3x2F.Identity;

Matrix transform = m_transformAdapter.Transform;
RectangleF clientRect = AdaptedControl.ClientRectangle;

// draw horizontal lines
// ChartUtil.DrawHorizontalGrid(transform, canvasRect, m_verticalGridSpacing, d2dControl.Theme.GridPen, d2dControl.D2dGraphics);

// draw vertical lines
// ChartUtil.DrawVerticalGrid(transform, canvasRect, m_horizontalGridSpacing, d2dControl.Theme.GridPen, d2dControl.D2dGraphics);

d2dControl.D2dGraphics.Transform = xform;

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ protected override void OnDragging(MouseEventArgs e)

private void UpdateBounds()
{
Matrix transform = GetTransform();
for (int i = 0; i < m_draggingItems.Length; i++)
using (Matrix transform = GetTransform())
{
Rectangle bounds = GdiUtil.Transform(transform, m_originalBounds[i]);//all in world coordinates
m_layoutContext.SetBounds(m_draggingItems[i], bounds, BoundsSpecified.All);
for (int i = 0; i < m_draggingItems.Length; i++)
{
Rectangle bounds = GdiUtil.Transform(transform, m_originalBounds[i]); //all in world coordinates
m_layoutContext.SetBounds(m_draggingItems[i], bounds, BoundsSpecified.All);
}
}
}

Expand Down Expand Up @@ -269,7 +271,7 @@ private void SetCursor(Direction direction)
}

// Create a transform matrix from m_startingBounds to a new boundary rectangle,
// based on the current mouse drag.
// based on the current mouse drag. Caller must dispose of the resulting matrix.
private Matrix GetTransform()
{
Matrix transform = m_transformAdapter.Transform;
Expand Down
9 changes: 8 additions & 1 deletion Framework/Atf.Gui.WinForms/Controls/CanvasControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ public int DragThreshold
/// client coordinates</summary>
public virtual Matrix Transform
{
get { return Sce.Atf.GdiUtil.GetTransform(m_scroll, m_xZoom, m_yZoom); }
get
{
m_transform.Reset();
m_transform.Translate(m_scroll.X, m_scroll.Y);
m_transform.Scale(m_xZoom, m_yZoom);
return m_transform;
}
}
private readonly Matrix m_transform = new Matrix();

/// <summary>
/// Gets whether interactive zooming is uniform on x and y axes</summary>
Expand Down
74 changes: 46 additions & 28 deletions Framework/Atf.Gui.WinForms/Controls/ColorPicker/ColorBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ private void ClearMarker()

break;
}

g.Dispose();
}


Expand Down Expand Up @@ -484,6 +486,8 @@ private void DrawMarker(int x, int y, bool Unconditional) // **
g.DrawEllipse(pen, x - 3, y - 3, 10, 10); // Draw the marker : 11 x 11 circle

DrawBorder(); // Force the border to be redrawn, just in case the marker has been drawn over it.

g.Dispose();
}


Expand All @@ -509,36 +513,38 @@ private void DrawBorder()

pencil = new Pen(Color.Black);
g.DrawRectangle(pencil, 1, 1, Width - 3, Height - 3); // Draw inner black rectangle

g.Dispose();
}


/// <summary>
/// Evaluates the drawing style of the control and calls the appropriate
/// drawing function for content</summary>
private void DrawContent()
{
switch (m_eDrawStyle)
{
case eDrawStyle.Hue :
Draw_Style_Hue();
break;
case eDrawStyle.Saturation :
Draw_Style_Saturation();
break;
case eDrawStyle.Brightness :
Draw_Style_Luminance();
break;
case eDrawStyle.Red :
Draw_Style_Red();
break;
case eDrawStyle.Green :
Draw_Style_Green();
break;
case eDrawStyle.Blue :
Draw_Style_Blue();
break;
}
}
///// <summary>
///// Evaluates the drawing style of the control and calls the appropriate
///// drawing function for content</summary>
//private void DrawContent()
//{
// switch (m_eDrawStyle)
// {
// case eDrawStyle.Hue :
// Draw_Style_Hue();
// break;
// case eDrawStyle.Saturation :
// Draw_Style_Saturation();
// break;
// case eDrawStyle.Brightness :
// Draw_Style_Luminance();
// break;
// case eDrawStyle.Red :
// Draw_Style_Red();
// break;
// case eDrawStyle.Green :
// Draw_Style_Green();
// break;
// case eDrawStyle.Blue :
// Draw_Style_Blue();
// break;
// }
//}


/// <summary>
Expand All @@ -562,6 +568,8 @@ private void Draw_Style_Hue()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, Width - 4, 1), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 0, false);
g.FillRectangle(br,new Rectangle(2,i + 2, Width - 4, 1));
}

g.Dispose();
}


Expand All @@ -586,6 +594,8 @@ private void Draw_Style_Saturation()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, 1, Height - 4), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false);
g.FillRectangle(br,new Rectangle(i + 2, 2, 1, Height - 4));
}

g.Dispose();
}


Expand All @@ -610,6 +620,8 @@ private void Draw_Style_Luminance()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, 1, Height - 4), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false);
g.FillRectangle(br,new Rectangle(i + 2, 2, 1, Height - 4));
}

g.Dispose();
}


Expand All @@ -629,6 +641,8 @@ private void Draw_Style_Red()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, Width - 4, 1), Color.FromArgb(red, green, 0), Color.FromArgb(red, green, 255), 0, false);
g.FillRectangle(br,new Rectangle(2,i + 2, Width - 4, 1));
}

g.Dispose();
}


Expand All @@ -648,6 +662,8 @@ private void Draw_Style_Green()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, Width - 4, 1), Color.FromArgb(red, green, 0), Color.FromArgb(red, green, 255), 0, false);
g.FillRectangle(br,new Rectangle(2,i + 2, Width - 4, 1));
}

g.Dispose();
}


Expand All @@ -667,11 +683,13 @@ private void Draw_Style_Blue()
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, Width - 4, 1), Color.FromArgb(0, green, blue), Color.FromArgb(255, green, blue), 0, false);
g.FillRectangle(br,new Rectangle(2,i + 2, Width - 4, 1));
}

g.Dispose();
}


/// <summary>
/// Redraws the entire control, calling all the functions neccessary</summary>
/// Redraws the entire control, calling all the functions necessary</summary>
private void Redraw_Control()
{
DrawBorder();
Expand Down
Loading

0 comments on commit bc26641

Please sign in to comment.