Skip to content

Commit 70bf1cc

Browse files
committed
Fix leaks due to missing using or Dispose()
1 parent e62dcef commit 70bf1cc

File tree

12 files changed

+166
-121
lines changed

12 files changed

+166
-121
lines changed

Rubberduck.Core/Common/RubberduckHooks.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ public class RubberduckHooks : SubclassingWindow, IRubberduckHooks
1818
private readonly IList<IAttachable> _hooks = new List<IAttachable>();
1919
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2020

21+
private static IntPtr GetVbeMainWindowPtr(IVBE vbe)
22+
{
23+
using (var window = vbe.MainWindow)
24+
{
25+
return (IntPtr)window.HWnd;
26+
}
27+
}
28+
2129
public RubberduckHooks(IVBE vbe, IGeneralConfigService config, HotkeyFactory hotkeyFactory, AutoCompleteService autoComplete)
22-
: base((IntPtr)vbe.MainWindow.HWnd, (IntPtr)vbe.MainWindow.HWnd)
30+
: base(GetVbeMainWindowPtr(vbe), GetVbeMainWindowPtr(vbe))
2331
{
2432
_config = config;
2533
_hotkeyFactory = hotkeyFactory;

Rubberduck.Core/Properties/Settings.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<AutoGen>True</AutoGen>
124124
<DesignTimeSharedInput>True</DesignTimeSharedInput>
125125
<DependentUpon>Settings.settings</DependentUpon>
126+
<DesignTime>True</DesignTime>
126127
</Compile>
127128
<None Include="Properties\Settings.settings">
128129
<Generator>PublicSettingsSingleFileGenerator</Generator>

Rubberduck.Core/UI/Command/ExportAllCommand.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using NLog;
44
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
55
using Rubberduck.Navigation.CodeExplorer;
6-
using Rubberduck.UI.CodeExplorer.Commands;
76
using Rubberduck.Resources;
87
using Rubberduck.VBEditor.SafeComWrappers;
98

@@ -32,17 +31,32 @@ protected override bool EvaluateCanExecute(object parameter)
3231
return false;
3332
}
3433

35-
var projectNode = parameter as CodeExplorerProjectViewModel;
36-
37-
var project = parameter as IVBProject;
38-
39-
return Evaluate(projectNode?.Declaration.Project ?? project ?? _vbe.ActiveVBProject);
34+
switch (parameter)
35+
{
36+
case CodeExplorerProjectViewModel projectNode:
37+
return Evaluate(projectNode.Declaration.Project);
38+
case IVBProject project:
39+
return Evaluate(project);
40+
}
4041

42+
using (var activeProject = _vbe.ActiveVBProject)
43+
{
44+
return Evaluate(activeProject);
45+
}
4146
}
4247

4348
private bool Evaluate(IVBProject project)
4449
{
45-
return project != null && !project.IsWrappingNullReference && project.VBComponents.Count > 0;
50+
if (project == null || project.IsWrappingNullReference)
51+
{
52+
return false;
53+
}
54+
55+
using (var compontents = project.VBComponents)
56+
{
57+
return compontents.Count > 0;
58+
}
59+
4660
}
4761

4862
protected override void OnExecute(object parameter)

Rubberduck.Core/UI/Command/FindAllReferencesCommand.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,24 +192,24 @@ private Declaration FindTarget(object parameter)
192192
return declaration;
193193
}
194194

195-
bool findDesigner;
196195
using (var activePane = _vbe.ActiveCodePane)
197196
{
197+
bool findDesigner;
198198
using (var selectedComponent = _vbe.SelectedVBComponent)
199199
{
200-
findDesigner = activePane != null && !activePane.IsWrappingNullReference
201-
&& (selectedComponent?.HasDesigner ?? false);
200+
findDesigner = activePane != null && !activePane.IsWrappingNullReference
201+
&& (selectedComponent?.HasDesigner ?? false);
202202
}
203-
}
204203

205-
return findDesigner
206-
? FindFormDesignerTarget()
207-
: FindCodePaneTarget();
204+
return findDesigner
205+
? FindFormDesignerTarget()
206+
: FindCodePaneTarget(activePane);
207+
}
208208
}
209209

210-
private Declaration FindCodePaneTarget()
210+
private Declaration FindCodePaneTarget(ICodePane codePane)
211211
{
212-
return _state.FindSelectedDeclaration(_vbe.ActiveCodePane);
212+
return _state.FindSelectedDeclaration(codePane);
213213
}
214214

215215
private Declaration FindFormDesignerTarget(QualifiedModuleName? qualifiedModuleName = null)

Rubberduck.Core/UI/Command/IndentCurrentProjectCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public IndentCurrentProjectCommand(IVBE vbe, IIndenter indenter, RubberduckParse
2323

2424
protected override bool EvaluateCanExecute(object parameter)
2525
{
26-
return !_vbe.ActiveVBProject.IsWrappingNullReference && _vbe.ActiveVBProject.Protection != ProjectProtection.Locked;
26+
using (var vbProject = _vbe.ActiveVBProject)
27+
{
28+
return !vbProject.IsWrappingNullReference &&
29+
vbProject.Protection != ProjectProtection.Locked;
30+
}
2731
}
2832

2933
protected override void OnExecute(object parameter)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected override bool EvaluateCanExecute(object parameter)
2626
Declaration target;
2727
using (var activePane = Vbe.ActiveCodePane)
2828
{
29-
if (Vbe.ActiveCodePane == null || activePane.IsWrappingNullReference)
29+
if (activePane == null || activePane.IsWrappingNullReference)
3030
{
3131
return false;
3232
}

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,35 @@ private Declaration GetTarget(QualifiedModuleName? qualifiedModuleName = null)
5757
{
5858
projectId = activeProject.ProjectId;
5959
}
60-
var component = Vbe.SelectedVBComponent;
6160

62-
if (component?.HasDesigner ?? false)
61+
using (var component = Vbe.SelectedVBComponent)
6362
{
64-
DeclarationType selectedType;
65-
string selectedName;
66-
using (var selectedControls = component.SelectedControls)
63+
if (component?.HasDesigner ?? false)
6764
{
68-
var selectedCount = selectedControls.Count;
69-
if (selectedCount > 1)
65+
DeclarationType selectedType;
66+
string selectedName;
67+
using (var selectedControls = component.SelectedControls)
7068
{
71-
return null;
69+
var selectedCount = selectedControls.Count;
70+
if (selectedCount > 1)
71+
{
72+
return null;
73+
}
74+
75+
// Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
76+
(selectedType, selectedName) = selectedCount == 0
77+
? (DeclarationType.ClassModule, component.Name)
78+
: (DeclarationType.Control, selectedControls[0].Name);
7279
}
7380

74-
// Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
75-
(selectedType, selectedName) = selectedCount == 0
76-
? (DeclarationType.ClassModule, component.Name)
77-
: (DeclarationType.Control, selectedControls[0].Name);
81+
return _state.DeclarationFinder
82+
.MatchName(selectedName)
83+
.SingleOrDefault(m => m.ProjectId == projectId
84+
&& m.DeclarationType.HasFlag(selectedType)
85+
&& m.ComponentName == component.Name);
7886
}
87+
}
7988

80-
return _state.DeclarationFinder
81-
.MatchName(selectedName)
82-
.SingleOrDefault(m => m.ProjectId == projectId
83-
&& m.DeclarationType.HasFlag(selectedType)
84-
&& m.ComponentName == component.Name);
85-
}
8689
return null;
8790
}
8891

Rubberduck.Core/UI/DockableToolwindowPresenter.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ private IWindow CreateToolWindow(IDockableUserControl control)
5353
{
5454
using (var windows = _vbe.Windows)
5555
{
56-
var info = windows.CreateToolWindow(_addin, RubberduckProgId.DockableWindowHostProgId, control.Caption, control.ClassId);
56+
var info = windows.CreateToolWindow(_addin, RubberduckProgId.DockableWindowHostProgId,
57+
control.Caption, control.ClassId);
5758
_userControlObject = info.UserControl;
5859
toolWindow = info.ToolWindow;
5960
}
@@ -68,14 +69,17 @@ private IWindow CreateToolWindow(IDockableUserControl control)
6869
Logger.Error(exception);
6970
throw;
7071
}
71-
72+
7273
toolWindow.IsVisible = true; //window resizing doesn't work without this
7374
EnsureMinimumWindowSize(toolWindow);
7475
toolWindow.IsVisible = _settings != null && _settings.IsWindowVisible(this);
7576

7677
// currently we always inject _DockableToolWindowHost from Rubberduck.Main.
7778
// that method is not exposed in any of the interfaces we know, though, so we need to invoke it blindly
78-
((dynamic)_userControlObject).AddUserControl(control as UserControl, new IntPtr(_vbe.MainWindow.HWnd));
79+
using (var mainWindow = _vbe.MainWindow)
80+
{
81+
((dynamic) _userControlObject).AddUserControl(control as UserControl, new IntPtr(mainWindow.HWnd));
82+
}
7983

8084
return toolWindow;
8185
}

0 commit comments

Comments
 (0)