Skip to content

Commit 5539581

Browse files
committed
Additional leaks plugged
1 parent 0aaf7c1 commit 5539581

File tree

9 files changed

+97
-65
lines changed

9 files changed

+97
-65
lines changed

Rubberduck.Core/UI/CodeExplorer/Commands/AddMDIFormCommand.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ private bool EvaluateCanExecuteCore(IVBProject project, CodeExplorerItemViewMode
4545
return false;
4646
}
4747

48-
foreach (var component in project.VBComponents)
48+
using (var components = project.VBComponents)
4949
{
50-
using (component)
50+
foreach (var component in components)
5151
{
52-
if (component.Type == ComponentType.MDIForm)
52+
using (component)
5353
{
54-
// Only one MDI Form allowed per project
55-
return false;
54+
if (component.Type == ComponentType.MDIForm)
55+
{
56+
// Only one MDI Form allowed per project
57+
return false;
58+
}
5659
}
5760
}
5861
}

Rubberduck.Core/UI/Command/ExportAllCommand.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,28 @@ protected override void OnExecute(object parameter)
6565

6666
var vbproject = parameter as IVBProject;
6767

68-
var project = projectNode?.Declaration.Project ?? vbproject ?? _vbe.ActiveVBProject;
69-
70-
var desc = string.Format(RubberduckUI.ExportAllCommand_SaveAsDialog_Title, project.Name);
71-
72-
// If .GetDirectoryName is passed an empty string for a RootFolder,
73-
// it defaults to the Documents library (Win 7+) or equivalent.
74-
var path = string.Empty;
75-
if (!string.IsNullOrWhiteSpace(project.FileName))
68+
using (var activeProject = _vbe.ActiveVBProject)
7669
{
77-
path = Path.GetDirectoryName(project.FileName);
78-
}
70+
var project = projectNode?.Declaration.Project ?? vbproject ?? activeProject;
7971

80-
using (var _folderBrowser = _factory.CreateFolderBrowser(desc, true, path))
81-
{
82-
var result = _folderBrowser.ShowDialog();
72+
var desc = string.Format(RubberduckUI.ExportAllCommand_SaveAsDialog_Title, project.Name);
8373

84-
if (result == DialogResult.OK)
74+
// If .GetDirectoryName is passed an empty string for a RootFolder,
75+
// it defaults to the Documents library (Win 7+) or equivalent.
76+
var path = string.Empty;
77+
if (!string.IsNullOrWhiteSpace(project.FileName))
8578
{
86-
project.ExportSourceFiles(_folderBrowser.SelectedPath);
79+
path = Path.GetDirectoryName(project.FileName);
80+
}
81+
82+
using (var _folderBrowser = _factory.CreateFolderBrowser(desc, true, path))
83+
{
84+
var result = _folderBrowser.ShowDialog();
85+
86+
if (result == DialogResult.OK)
87+
{
88+
project.ExportSourceFiles(_folderBrowser.SelectedPath);
89+
}
8790
}
8891
}
8992
}

Rubberduck.Core/UI/Command/FindAllReferencesCommand.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,36 @@ private Declaration FindFormDesignerTarget(QualifiedModuleName? qualifiedModuleN
224224
{
225225
projectId = activeProject.ProjectId;
226226
}
227-
var component = _vbe.SelectedVBComponent;
228227

229-
if (component?.HasDesigner ?? false)
228+
using (var component = _vbe.SelectedVBComponent)
230229
{
231-
DeclarationType selectedType;
232-
string selectedName;
233-
using (var selectedControls = component.SelectedControls)
230+
if (component?.HasDesigner ?? false)
234231
{
235-
var selectedCount = selectedControls.Count;
236-
if (selectedCount > 1)
232+
DeclarationType selectedType;
233+
string selectedName;
234+
using (var selectedControls = component.SelectedControls)
237235
{
238-
return null;
236+
var selectedCount = selectedControls.Count;
237+
if (selectedCount > 1)
238+
{
239+
return null;
240+
}
241+
242+
// Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
243+
(selectedType, selectedName) = selectedCount == 0
244+
? (DeclarationType.ClassModule, component.Name)
245+
: (DeclarationType.Control, selectedControls[0].Name);
239246
}
240247

241-
// Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
242-
(selectedType, selectedName) = selectedCount == 0
243-
? (DeclarationType.ClassModule, component.Name)
244-
: (DeclarationType.Control, selectedControls[0].Name);
248+
return _state.DeclarationFinder
249+
.MatchName(selectedName)
250+
.SingleOrDefault(m => m.ProjectId == projectId
251+
&& m.DeclarationType.HasFlag(selectedType)
252+
&& m.ComponentName == component.Name);
245253
}
246-
return _state.DeclarationFinder
247-
.MatchName(selectedName)
248-
.SingleOrDefault(m => m.ProjectId == projectId
249-
&& m.DeclarationType.HasFlag(selectedType)
250-
&& m.ComponentName == component.Name);
254+
255+
return null;
251256
}
252-
return null;
253257
}
254258

255259
private Declaration FindFormDesignerTarget(QualifiedModuleName qualifiedModuleName)

Rubberduck.Core/UI/Command/Refactorings/CodePaneRefactorRenameCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected override void OnExecute(object parameter)
5656
}
5757
else
5858
{
59-
target = _state.FindSelectedDeclaration(Vbe.ActiveCodePane);
59+
target = _state.FindSelectedDeclaration(activePane);
6060
}
6161
}
6262

Rubberduck.UnitTesting/UnitTesting/VBEInteraction.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public void EnsureProjectReferencesUnitTesting(IVBProject project)
9696

9797
if (!ReferenceWithPathExists(references, referencePath))
9898
{
99-
references.AddFromFile(referencePath);
99+
// AddFromFile returns a new wrapped reference so we must
100+
// ensure it is disposed properly.
101+
using (references.AddFromFile(referencePath)) { }
100102
}
101103
}
102104
}

Rubberduck.VBEEditor/ComManagement/ComSafeBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private struct TraceData
8888
/// </summary>
8989
private const int StackTraceNumberOfElementsToSkipOnRemoval = 6;
9090
private const int StackTrackNumberOfElementsToSkipOnAddUpdate = 8;
91-
private const int StackTraceDepth = 5;
91+
private const int StackTraceDepth = 10;
9292

9393
/// <inheritdoc cref="IComSafe.Serialize"/>
9494
public void Serialize(string targetDirectory)

Rubberduck.VBEditor.VB6/SafeComWrappers/VB/CodePane.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,22 @@ private void ForceFocus()
102102

103103
Show();
104104

105-
var window = VBE.MainWindow;
106-
var mainWindowHandle = window.Handle();
107-
var handle = window.Handle().FindChildWindow(Window.Caption);
108-
109-
if (handle != IntPtr.Zero)
110-
{
111-
NativeMethods.ActivateWindow(handle, mainWindowHandle);
112-
}
113-
else
105+
using (var vbe = VBE)
106+
using (var mainWindow = vbe.MainWindow)
107+
using (var window = Window)
114108
{
115-
System.Diagnostics.Debug.WriteLine("CodePane.ForceFocus() failed to get a handle on the MainWindow.");
109+
var mainWindowHandle = mainWindow.Handle();
110+
var handle = mainWindow.Handle().FindChildWindow(window.Caption);
111+
112+
if (handle != IntPtr.Zero)
113+
{
114+
NativeMethods.ActivateWindow(handle, mainWindowHandle);
115+
}
116+
else
117+
{
118+
System.Diagnostics.Debug.WriteLine(
119+
"CodePane.ForceFocus() failed to get a handle on the MainWindow.");
120+
}
116121
}
117122
}
118123

Rubberduck.VBEditor.VBA/SafeComWrappers/VB/CodePane.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,22 @@ private void ForceFocus()
102102

103103
Show();
104104

105-
var window = VBE.MainWindow;
106-
var mainWindowHandle = window.Handle();
107-
var handle = window.Handle().FindChildWindow(Window.Caption);
108-
109-
if (handle != IntPtr.Zero)
105+
using (var vbe = VBE)
106+
using (var mainWindow = vbe.MainWindow)
107+
using (var window = Window)
110108
{
111-
NativeMethods.ActivateWindow(handle, mainWindowHandle);
112-
}
113-
else
114-
{
115-
System.Diagnostics.Debug.WriteLine("CodePane.ForceFocus() failed to get a handle on the MainWindow.");
109+
var mainWindowHandle = mainWindow.Handle();
110+
var handle = mainWindow.Handle().FindChildWindow(window.Caption);
111+
112+
if (handle != IntPtr.Zero)
113+
{
114+
NativeMethods.ActivateWindow(handle, mainWindowHandle);
115+
}
116+
else
117+
{
118+
System.Diagnostics.Debug.WriteLine(
119+
"CodePane.ForceFocus() failed to get a handle on the MainWindow.");
120+
}
116121
}
117122
}
118123

Rubberduck.VBEditor.VBA/SafeComWrappers/VB/VBProject.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ public override int GetHashCode()
100100

101101
public IReadOnlyList<string> ComponentNames()
102102
{
103-
return VBComponents.Select(component => component.Name).ToArray();
103+
var names = new List<string>();
104+
using (var components = VBComponents)
105+
{
106+
foreach(var component in components)
107+
using (component)
108+
{
109+
names.Add(component.Name);
110+
}
111+
112+
return names.ToArray();
113+
}
104114
}
105115

106116
public void AssignProjectId()
@@ -174,9 +184,9 @@ public string ProjectDisplayName
174184
return _displayName;
175185
}
176186

177-
var vbe = VBE;
178-
var activeProject = vbe.ActiveVBProject;
179-
var mainWindow = vbe.MainWindow;
187+
using (var vbe = VBE)
188+
using (var activeProject = vbe.ActiveVBProject)
189+
using (var mainWindow = vbe.MainWindow)
180190
{
181191
try
182192
{

0 commit comments

Comments
 (0)