Skip to content

Commit 372fbe6

Browse files
committed
More CodeModule disposal
1 parent 5c216b3 commit 372fbe6

File tree

12 files changed

+271
-148
lines changed

12 files changed

+271
-148
lines changed

RetailCoder.VBE/Navigation/RegexSearchReplace/RegexSearchReplace.cs

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ private void SetSelection(RegexSearchResult item)
9494

9595
private List<RegexSearchResult> SearchSelection(string searchPattern)
9696
{
97-
var pane = _vbe.ActiveCodePane;
98-
var module = pane.CodeModule;
97+
using (var pane = _vbe.ActiveCodePane)
9998
{
100-
var results = GetResultsFromModule(module, searchPattern);
101-
return results.Where(r => pane.Selection.Contains(r.Selection)).ToList();
99+
using (var module = pane.CodeModule)
100+
{
101+
var results = GetResultsFromModule(module, searchPattern);
102+
return results.Where(r => pane.Selection.Contains(r.Selection)).ToList();
103+
}
102104
}
103105
}
104106

@@ -115,34 +117,56 @@ private List<RegexSearchResult> SearchCurrentBlock(string searchPattern)
115117
};
116118

117119
var state = _parser.State;
118-
var pane = _vbe.ActiveCodePane;
119-
var module = pane.CodeModule;
120+
using (var pane = _vbe.ActiveCodePane)
120121
{
121-
var results = GetResultsFromModule(module, searchPattern);
122+
using (var module = pane.CodeModule)
123+
{
124+
var results = GetResultsFromModule(module, searchPattern);
122125

123-
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(module.Parent), pane.Selection);
124-
dynamic block = state.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context.Parent;
125-
var selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line, block.Stop.Column);
126-
return results.Where(r => selection.Contains(r.Selection)).ToList();
126+
using (var moduleParent = module.Parent)
127+
{
128+
var qualifiedSelection =
129+
new QualifiedSelection(new QualifiedModuleName(moduleParent), pane.Selection);
130+
dynamic block = state.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context
131+
.Parent;
132+
var selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line,
133+
block.Stop.Column);
134+
135+
return results.Where(r => selection.Contains(r.Selection)).ToList();
136+
}
137+
}
127138
}
128139
}
129140

130141
private List<RegexSearchResult> SearchCurrentFile(string searchPattern)
131142
{
132-
var pane = _vbe.ActiveCodePane;
143+
using (var pane = _vbe.ActiveCodePane)
133144
{
134-
return GetResultsFromModule(pane.CodeModule, searchPattern).ToList();
145+
using (var codeModule = pane.CodeModule)
146+
{
147+
return GetResultsFromModule(codeModule, searchPattern).ToList();
148+
}
135149
}
136150
}
137151

138152
private List<RegexSearchResult> SearchOpenFiles(string searchPattern)
139153
{
140154
var results = new List<RegexSearchResult>();
141-
var panes = _vbe.CodePanes;
155+
using (var panes = _vbe.CodePanes)
142156
{
143157
foreach (var codePane in panes)
144158
{
145-
results.AddRange(GetResultsFromModule(codePane.CodeModule, searchPattern));
159+
try
160+
{
161+
using (var codeModule = codePane.CodeModule)
162+
{
163+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
164+
}
165+
}
166+
finally
167+
{
168+
codePane.Dispose();
169+
}
146170
}
147171

148172
return results;
@@ -152,15 +176,26 @@ private List<RegexSearchResult> SearchOpenFiles(string searchPattern)
152176
private List<RegexSearchResult> SearchCurrentProject(string searchPattern)
153177
{
154178
var results = new List<RegexSearchResult>();
155-
var project = _vbe.ActiveVBProject;
156-
var components = project.VBComponents;
179+
using (var project = _vbe.ActiveVBProject)
157180
{
158-
foreach (var component in components)
181+
using (var components = project.VBComponents)
159182
{
160-
results.AddRange(GetResultsFromModule(component.CodeModule, searchPattern));
183+
foreach (var component in components)
184+
{
185+
try
186+
{
187+
using (var codeModule = component.CodeModule)
188+
{
189+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
190+
}
191+
}
192+
finally
193+
{
194+
component.Dispose();
195+
}
196+
}
197+
return results;
161198
}
162-
163-
return results;
164199
}
165200
}
166201

@@ -171,27 +206,38 @@ private List<RegexSearchResult> SearchOpenProjects(string searchPattern)
171206
{
172207
foreach (var project in projects)
173208
{
174-
if (project.Protection == ProjectProtection.Locked)
209+
try
175210
{
176-
project.Dispose();
177-
continue;
178-
}
179-
using (var components = project.VBComponents)
180-
{
181-
foreach (var component in components)
211+
if (project.Protection == ProjectProtection.Locked)
182212
{
183-
using (var codeModule = component.CodeModule)
213+
continue;
214+
}
215+
216+
using (var components = project.VBComponents)
217+
{
218+
foreach (var component in components)
184219
{
185-
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
220+
try
221+
{
222+
using (var codeModule = component.CodeModule)
223+
{
224+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
225+
}
226+
}
227+
finally
228+
{
229+
component.Dispose();
230+
}
186231
}
187-
component.Dispose();
188232
}
189233
}
190-
project.Dispose();
234+
finally
235+
{
236+
project.Dispose();
237+
}
191238
}
192-
193-
return results;
194239
}
240+
return results;
195241
}
196242
}
197243
}

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ public void Refactor()
4141
}
4242

4343
QualifiedSelection? oldSelection = null;
44-
var pane = _vbe.ActiveCodePane;
45-
var module = pane.CodeModule;
46-
if (!module.IsWrappingNullReference)
44+
using (var pane = _vbe.ActiveCodePane)
4745
{
48-
oldSelection = module.GetQualifiedSelection();
49-
}
46+
using (var module = pane.CodeModule)
47+
{
48+
if (!module.IsWrappingNullReference)
49+
{
50+
oldSelection = module.GetQualifiedSelection();
51+
}
52+
}
5053

51-
RemoveParameters();
54+
RemoveParameters();
5255

53-
if (oldSelection.HasValue)
54-
{
55-
pane.Selection = oldSelection.Value.Selection;
56+
if (oldSelection.HasValue)
57+
{
58+
pane.Selection = oldSelection.Value.Selection;
59+
}
5660
}
5761

5862
_model.State.OnParseRequested(this);
@@ -116,7 +120,6 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references, Decla
116120
{
117121
foreach (var reference in references.Where(item => item.Context != method.Context))
118122
{
119-
var module = reference.QualifiedModuleName.Component.CodeModule;
120123
VBAParser.ArgumentListContext argumentList = null;
121124
var callStmt = ParserRuleContextHelper.GetParent<VBAParser.CallStmtContext>(reference.Context);
122125
if (callStmt != null)
@@ -139,7 +142,10 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references, Decla
139142
continue;
140143
}
141144

142-
RemoveCallArguments(argumentList, module);
145+
using (var module = reference.QualifiedModuleName.Component.CodeModule)
146+
{
147+
RemoveCallArguments(argumentList, module);
148+
}
143149
}
144150
}
145151

RetailCoder.VBE/Refactorings/Rename/RenamePresenterFactory.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ public RenamePresenterFactory(IVBE vbe, IRefactoringDialog<RenameViewModel> view
2121

2222
public RenamePresenter Create()
2323
{
24-
var codePane = _vbe.ActiveCodePane;
25-
var qualifiedSelection = codePane.IsWrappingNullReference
26-
? new QualifiedSelection()
27-
: new QualifiedSelection(new QualifiedModuleName(codePane.CodeModule.Parent), codePane.Selection);
28-
24+
QualifiedSelection qualifiedSelection;
25+
using (var codePane = _vbe.ActiveCodePane)
26+
{
27+
using (var codeModule = codePane.CodeModule)
28+
{
29+
qualifiedSelection = codePane.IsWrappingNullReference
30+
? new QualifiedSelection()
31+
: new QualifiedSelection(new QualifiedModuleName(codeModule.Parent), codePane.Selection);
32+
}
33+
}
2934
return new RenamePresenter(_view, new RenameModel(_vbe, _state, qualifiedSelection));
3035
}
3136
}

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersRefactoring.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,23 @@ public void Refactor()
4141
return;
4242
}
4343

44-
var pane = _vbe.ActiveCodePane;
45-
if (!pane.IsWrappingNullReference)
44+
using (var pane = _vbe.ActiveCodePane)
4645
{
47-
QualifiedSelection? oldSelection;
48-
var module = pane.CodeModule;
46+
if (!pane.IsWrappingNullReference)
4947
{
50-
oldSelection = module.GetQualifiedSelection();
51-
}
48+
QualifiedSelection? oldSelection;
49+
using (var module = pane.CodeModule)
50+
{
51+
oldSelection = module.GetQualifiedSelection();
52+
}
5253

53-
AdjustReferences(_model.TargetDeclaration.References);
54-
AdjustSignatures();
54+
AdjustReferences(_model.TargetDeclaration.References);
55+
AdjustSignatures();
5556

56-
if (oldSelection.HasValue)
57-
{
58-
pane.Selection = oldSelection.Value.Selection;
57+
if (oldSelection.HasValue)
58+
{
59+
pane.Selection = oldSelection.Value.Selection;
60+
}
5961
}
6062
}
6163

@@ -69,7 +71,7 @@ public void Refactor()
6971

7072
public void Refactor(QualifiedSelection target)
7173
{
72-
var pane = _vbe.ActiveCodePane;
74+
using (var pane = _vbe.ActiveCodePane)
7375
{
7476
pane.Selection = target.Selection;
7577
Refactor();
@@ -83,7 +85,7 @@ public void Refactor(Declaration target)
8385
throw new ArgumentException("Invalid declaration type");
8486
}
8587

86-
var pane = _vbe.ActiveCodePane;
88+
using (var pane = _vbe.ActiveCodePane)
8789
{
8890
pane.Selection = target.QualifiedSelection.Selection;
8991
Refactor();

RetailCoder.VBE/UI/CodeExplorer/Commands/AddComponentCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void AddComponent(CodeExplorerItemViewModel node, ComponentType type)
3232
{
3333
using (var components = node != null
3434
? GetDeclaration(node).Project.VBComponents
35-
: ComponentsColectionFromActiveProject())
35+
: ComponentsCollectionFromActiveProject())
3636
{
3737
var folderAnnotation = $"'@Folder(\"{GetFolder(node)}\")";
3838

@@ -46,7 +46,7 @@ public void AddComponent(CodeExplorerItemViewModel node, ComponentType type)
4646
}
4747
}
4848

49-
private IVBComponents ComponentsColectionFromActiveProject()
49+
private IVBComponents ComponentsCollectionFromActiveProject()
5050
{
5151
using (var activeProject = _vbe.ActiveVBProject)
5252
{

RetailCoder.VBE/UI/Command/Refactorings/RefactorReorderParametersCommand.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,26 @@ protected override bool EvaluateCanExecute(object parameter)
6060

6161
protected override void OnExecute(object parameter)
6262
{
63-
var pane = Vbe.ActiveCodePane;
64-
var module = pane.CodeModule;
63+
using (var pane = Vbe.ActiveCodePane)
6564
{
6665
if (pane.IsWrappingNullReference)
6766
{
6867
return;
6968
}
70-
var selection = new QualifiedSelection(new QualifiedModuleName(module.Parent), pane.Selection);
7169

72-
using (var view = new ReorderParametersDialog(new ReorderParametersViewModel(_state)))
70+
using (var module = pane.CodeModule)
7371
{
74-
var factory = new ReorderParametersPresenterFactory(Vbe, view, _state, _msgbox);
75-
var refactoring = new ReorderParametersRefactoring(Vbe, factory, _msgbox);
76-
refactoring.Refactor(selection);
72+
using (var component = module.Parent)
73+
{
74+
var selection = new QualifiedSelection(new QualifiedModuleName(component), pane.Selection); //The component does not get passed out. So, it is safe to dispose afterwards.
75+
76+
using (var view = new ReorderParametersDialog(new ReorderParametersViewModel(_state)))
77+
{
78+
var factory = new ReorderParametersPresenterFactory(Vbe, view, _state, _msgbox);
79+
var refactoring = new ReorderParametersRefactoring(Vbe, factory, _msgbox);
80+
refactoring.Refactor(selection);
81+
}
82+
}
7783
}
7884
}
7985
}

RetailCoder.VBE/UI/IdentifierReferences/IdentifierReferencesListDockablePresenter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ private void BindTarget(Declaration target)
2626

2727
public static void OnNavigateIdentifierReference(IdentifierReference reference)
2828
{
29-
reference.QualifiedModuleName.Component.CodeModule.CodePane.Selection = reference.Selection;
29+
using (var codeModule = reference.QualifiedModuleName.Component.CodeModule)
30+
{
31+
using (var codePane = codeModule.CodePane)
32+
{
33+
codePane.Selection = reference.Selection;
34+
}
35+
}
3036
}
3137

3238
private void ControlNavigate(object sender, ListItemActionEventArgs e)

0 commit comments

Comments
 (0)