Skip to content

Commit 5c216b3

Browse files
committed
Added disposal of temporary ICodeModule instances. (In the main project, this is not complete.)
1 parent 4fd5ed8 commit 5c216b3

File tree

23 files changed

+329
-181
lines changed

23 files changed

+329
-181
lines changed

RetailCoder.VBE/Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,23 @@ private void AddInterface()
100100

101101
AddInterfaceMembersToClass(rewriter);
102102

103-
var components = _model.TargetDeclaration.Project.VBComponents;
104-
var interfaceComponent = components.Add(ComponentType.ClassModule);
105-
var interfaceModule = interfaceComponent.CodeModule;
106-
interfaceComponent.Name = _model.InterfaceName;
107-
108-
var optionPresent = interfaceModule.CountOfLines > 1;
109-
if (!optionPresent)
103+
using (var components = _model.TargetDeclaration.Project.VBComponents)
110104
{
111-
interfaceModule.InsertLines(1, $"{Tokens.Option} {Tokens.Explicit}{Environment.NewLine}");
105+
using (var interfaceComponent = components.Add(ComponentType.ClassModule))
106+
{
107+
using (var interfaceModule = interfaceComponent.CodeModule)
108+
{
109+
interfaceComponent.Name = _model.InterfaceName;
110+
111+
var optionPresent = interfaceModule.CountOfLines > 1;
112+
if (!optionPresent)
113+
{
114+
interfaceModule.InsertLines(1, $"{Tokens.Option} {Tokens.Explicit}{Environment.NewLine}");
115+
}
116+
interfaceModule.InsertLines(3, GetInterfaceModuleBody());
117+
}
118+
}
112119
}
113-
interfaceModule.InsertLines(3, GetInterfaceModuleBody());
114120
}
115121

116122
private void AddInterfaceMembersToClass(IModuleRewriter rewriter)

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,23 @@ public void Refactor(QualifiedSelection selection)
7777
QualifiedSelection? oldSelection = null;
7878
if (_vbe.ActiveCodePane != null)
7979
{
80-
oldSelection = _vbe.ActiveCodePane.CodeModule.GetQualifiedSelection();
80+
using (var codePane = _vbe.ActiveCodePane)
81+
{
82+
oldSelection = codePane.GetQualifiedSelection();
83+
}
8184
}
8285

8386
ImplementMissingMembers(_state.GetRewriter(_targetClass));
8487

8588
if (oldSelection.HasValue)
8689
{
87-
var module = oldSelection.Value.QualifiedName.Component.CodeModule;
88-
var pane = module.CodePane;
89-
pane.Selection = oldSelection.Value.Selection;
90+
using (var module = oldSelection.Value.QualifiedName.Component.CodeModule)
91+
{
92+
using (var pane = module.CodePane)
93+
{
94+
pane.Selection = oldSelection.Value.Selection;
95+
}
96+
}
9097
}
9198

9299
_state.OnParseRequested(this);

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,27 @@ private bool IsValidTarget(Declaration target)
210210

211211
if (target.DeclarationType.HasFlag(DeclarationType.Control))
212212
{
213-
var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
214-
var control = module.Parent.Controls.FirstOrDefault(item => item.Name == target.IdentifierName);
215-
if (control == null)
213+
using (var controls = target.QualifiedName.QualifiedModuleName.Component.Controls)
216214
{
217-
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null control reference");
218-
return false;
215+
using (var control = controls.FirstOrDefault(item => item.Name == target.IdentifierName))
216+
{
217+
if (control == null)
218+
{
219+
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null control reference");
220+
return false;
221+
}
222+
}
219223
}
220224
}
221225
else if (target.DeclarationType.HasFlag(DeclarationType.Module))
222226
{
223-
var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
224-
if (module.IsWrappingNullReference)
227+
using (var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule)
225228
{
226-
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null Module reference");
227-
return false;
229+
if (module.IsWrappingNullReference)
230+
{
231+
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null Module reference");
232+
return false;
233+
}
228234
}
229235
}
230236
return true;
@@ -381,13 +387,17 @@ private void RenameVariable()
381387
{
382388
if (_model.Target.DeclarationType.HasFlag(DeclarationType.Control))
383389
{
384-
var module = _model.Target.QualifiedName.QualifiedModuleName.Component.CodeModule;
385-
var control = module.Parent.Controls.SingleOrDefault(item => item.Name == _model.Target.IdentifierName);
386-
Debug.Assert(control != null, $"input validation fail: unable to locate '{_model.Target.IdentifierName}' in Controls collection");
390+
using (var controls = _model.Target.QualifiedName.QualifiedModuleName.Component.Controls)
391+
{
392+
using (var control = controls.SingleOrDefault(item => item.Name == _model.Target.IdentifierName))
393+
{
394+
Debug.Assert(control != null,
395+
$"input validation fail: unable to locate '{_model.Target.IdentifierName}' in Controls collection");
387396

388-
control.Name = _model.NewName;
397+
control.Name = _model.NewName;
398+
}
399+
}
389400
RenameReferences(_model.Target, _model.NewName);
390-
391401
var controlEventHandlers = FindEventHandlersForControl(_model.Target);
392402
RenameDefinedFormatMembers(controlEventHandlers, _appendUnderscoreFormat);
393403
}
@@ -443,8 +453,11 @@ private void RenameModule()
443453
}
444454
else
445455
{
446-
Debug.Assert(!component.CodeModule.IsWrappingNullReference, "input validation fail: Attempting to rename an ICodeModule wrapping a null reference");
447-
component.CodeModule.Name = _model.NewName;
456+
using (var codeModule = component.CodeModule)
457+
{
458+
Debug.Assert(!codeModule.IsWrappingNullReference, "input validation fail: Attempting to rename an ICodeModule wrapping a null reference");
459+
codeModule.Name = _model.NewName;
460+
}
448461
}
449462
}
450463

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,28 @@ public bool CanAddComponent(CodeExplorerItemViewModel parameter)
3030

3131
public void AddComponent(CodeExplorerItemViewModel node, ComponentType type)
3232
{
33-
var components = node != null
33+
using (var components = node != null
3434
? GetDeclaration(node).Project.VBComponents
35-
: _vbe.ActiveVBProject.VBComponents;
35+
: ComponentsColectionFromActiveProject())
36+
{
37+
var folderAnnotation = $"'@Folder(\"{GetFolder(node)}\")";
3638

37-
var folderAnnotation = $"'@Folder(\"{GetFolder(node)}\")";
39+
using (var newComponent = components.Add(type))
40+
{
41+
using (var codeModule = newComponent.CodeModule)
42+
{
43+
codeModule.InsertLines(1, folderAnnotation);
44+
}
45+
}
46+
}
47+
}
3848

39-
var newComponent = components.Add(type);
40-
newComponent.CodeModule.InsertLines(1, folderAnnotation);
49+
private IVBComponents ComponentsColectionFromActiveProject()
50+
{
51+
using (var activeProject = _vbe.ActiveVBProject)
52+
{
53+
return activeProject.VBComponents;
54+
}
4155
}
4256

4357
private Declaration GetDeclaration(CodeExplorerItemViewModel node)

RetailCoder.VBE/UI/Command/AddTestModuleCommand.cs

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -159,61 +159,71 @@ protected override void OnExecute(object parameter)
159159
project.EnsureReferenceToAddInLibrary();
160160
}
161161

162-
var component = project.VBComponents.Add(ComponentType.StandardModule);
163-
var module = component.CodeModule;
164-
component.Name = GetNextTestModuleName(project);
165-
166-
var hasOptionExplicit = false;
167-
if (module.CountOfLines > 0 && module.CountOfDeclarationLines > 0)
168-
{
169-
hasOptionExplicit = module.GetLines(1, module.CountOfDeclarationLines).Contains("Option Explicit");
170-
}
171-
172-
var options = string.Concat(hasOptionExplicit ? string.Empty : "Option Explicit\r\n",
173-
"Option Private Module\r\n\r\n");
174-
175-
if (parameterIsModuleDeclaration)
162+
using(var components = project.VBComponents)
176163
{
177-
var moduleCodeBuilder = new StringBuilder();
178-
var declarationsToStub = GetDeclarationsToStub((Declaration)parameter);
179-
180-
foreach (var declaration in declarationsToStub)
164+
using (var component = components.Add(ComponentType.StandardModule))
181165
{
182-
var name = string.Empty;
183-
184-
switch (declaration.DeclarationType)
166+
using (var module = component.CodeModule)
185167
{
186-
case DeclarationType.Procedure:
187-
case DeclarationType.Function:
188-
name = declaration.IdentifierName;
189-
break;
190-
case DeclarationType.PropertyGet:
191-
name = $"Get{declaration.IdentifierName}";
192-
break;
193-
case DeclarationType.PropertyLet:
194-
name = $"Let{declaration.IdentifierName}";
195-
break;
196-
case DeclarationType.PropertySet:
197-
name = $"Set{declaration.IdentifierName}";
198-
break;
168+
component.Name = GetNextTestModuleName(project);
169+
170+
var hasOptionExplicit = false;
171+
if (module.CountOfLines > 0 && module.CountOfDeclarationLines > 0)
172+
{
173+
hasOptionExplicit = module.GetLines(1, module.CountOfDeclarationLines)
174+
.Contains("Option Explicit");
175+
}
176+
177+
var options = string.Concat(hasOptionExplicit ? string.Empty : "Option Explicit\r\n",
178+
"Option Private Module\r\n\r\n");
179+
180+
if (parameterIsModuleDeclaration)
181+
{
182+
var moduleCodeBuilder = new StringBuilder();
183+
var declarationsToStub = GetDeclarationsToStub((Declaration) parameter);
184+
185+
foreach (var declaration in declarationsToStub)
186+
{
187+
var name = string.Empty;
188+
189+
switch (declaration.DeclarationType)
190+
{
191+
case DeclarationType.Procedure:
192+
case DeclarationType.Function:
193+
name = declaration.IdentifierName;
194+
break;
195+
case DeclarationType.PropertyGet:
196+
name = $"Get{declaration.IdentifierName}";
197+
break;
198+
case DeclarationType.PropertyLet:
199+
name = $"Let{declaration.IdentifierName}";
200+
break;
201+
case DeclarationType.PropertySet:
202+
name = $"Set{declaration.IdentifierName}";
203+
break;
204+
}
205+
206+
var stub = AddTestMethodCommand.TestMethodTemplate.Replace(
207+
AddTestMethodCommand.NamePlaceholder, $"{name}_Test");
208+
moduleCodeBuilder.AppendLine(stub);
209+
}
210+
211+
module.AddFromString(options + GetTestModule(settings) + moduleCodeBuilder);
212+
}
213+
else
214+
{
215+
var defaultTestMethod = settings.DefaultTestStubInNewModule
216+
? AddTestMethodCommand.TestMethodTemplate.Replace(AddTestMethodCommand.NamePlaceholder,
217+
"TestMethod1")
218+
: string.Empty;
219+
220+
module.AddFromString(options + GetTestModule(settings) + defaultTestMethod);
221+
}
199222
}
200223

201-
var stub = AddTestMethodCommand.TestMethodTemplate.Replace(AddTestMethodCommand.NamePlaceholder, $"{name}_Test");
202-
moduleCodeBuilder.AppendLine(stub);
224+
component.Activate();
203225
}
204-
205-
module.AddFromString(options + GetTestModule(settings) + moduleCodeBuilder);
206-
}
207-
else
208-
{
209-
var defaultTestMethod = settings.DefaultTestStubInNewModule
210-
? AddTestMethodCommand.TestMethodTemplate.Replace(AddTestMethodCommand.NamePlaceholder, "TestMethod1")
211-
: string.Empty;
212-
213-
module.AddFromString(options + GetTestModule(settings) + defaultTestMethod);
214226
}
215-
216-
component.Activate();
217227
_state.OnParseRequested(this);
218228
}
219229

RetailCoder.VBE/UI/Command/NavigateCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ protected override void OnExecute(object parameter)
2424

2525
try
2626
{
27-
param.QualifiedName.Component.CodeModule.CodePane.Selection = param.Selection;
27+
using (var codeModule = param.QualifiedName.Component.CodeModule)
28+
{
29+
using (var codePane = codeModule.CodePane)
30+
{
31+
codePane.Selection = param.Selection;
32+
}
33+
}
2834
}
2935
catch (COMException)
3036
{

RetailCoder.VBE/UI/IdentifierReferences/ImplementationsListDockablePresenter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ private void BindTarget(IEnumerable<Declaration> implementations)
2828

2929
public static void OnNavigateImplementation(Declaration implementation)
3030
{
31-
implementation.QualifiedName.QualifiedModuleName.Component.CodeModule.CodePane.Selection = implementation.Selection;
31+
using (var codeModule = implementation.QualifiedName.QualifiedModuleName.Component.CodeModule)
32+
{
33+
using (var codePane = codeModule.CodePane)
34+
{
35+
codePane.Selection = implementation.Selection;
36+
}
37+
}
3238
}
3339

3440
private void ControlNavigate(object sender, ListItemActionEventArgs e)

RetailCoder.VBE/UI/ToDoItems/ToDoExplorerViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ public CommandBase RemoveCommand
149149
return;
150150
}
151151

152-
var module = _selectedItem.Selection.QualifiedName.Component.CodeModule;
152+
using (var module = _selectedItem.Selection.QualifiedName.Component.CodeModule)
153153
{
154154
var oldContent = module.GetLines(_selectedItem.Selection.Selection.StartLine, 1);
155155
var newContent = oldContent.Remove(_selectedItem.Selection.Selection.StartColumn - 1);
156156

157157
module.ReplaceLine(_selectedItem.Selection.Selection.StartLine, newContent);
158-
159-
RefreshCommand.Execute(null);
160158
}
161-
});
159+
RefreshCommand.Execute(null);
160+
}
161+
);
162162
}
163163
}
164164

RetailCoder.VBE/UnitTesting/UnitTestUtils.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ public static IEnumerable<TestMethod> GetTests(this IVBComponent component, IVBE
2626

2727
// apparently, sometimes it thinks the components are different but knows the modules are the same
2828
// if the modules are the same, then the component is the same as far as we are concerned
29-
return GetAllTests(vbe, state)
30-
.Where(test => test.Declaration.QualifiedName.QualifiedModuleName.Component.CodeModule.Equals(component.CodeModule));
29+
return GetAllTests(vbe, state).Where(test => TestCodeModuleEqualsComponentCodeModule(test, component));
30+
}
31+
32+
private static bool TestCodeModuleEqualsComponentCodeModule(TestMethod test, IVBComponent component)
33+
{
34+
using (var testCodeModule = test.Declaration.QualifiedName.QualifiedModuleName.Component.CodeModule)
35+
{
36+
using (var componentCodeModule = component.CodeModule)
37+
{
38+
return testCodeModule.Equals(componentCodeModule);
39+
}
40+
}
3141
}
3242

3343
public static bool IsTestMethod(RubberduckParserState state, Declaration item)

Rubberduck.Inspections/Concrete/ObsoleteCallStatementInspection.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
2929

3030
foreach (var context in Listener.Contexts.Where(context => !IsIgnoringInspectionResultFor(context.ModuleName, context.Context.Start.Line)))
3131
{
32-
var module = context.ModuleName.Component.CodeModule;
33-
var lines = module.GetLines(context.Context.Start.Line,
34-
context.Context.Stop.Line - context.Context.Start.Line + 1);
32+
string lines;
33+
using (var module = context.ModuleName.Component.CodeModule)
34+
{
35+
lines = module.GetLines(context.Context.Start.Line,
36+
context.Context.Stop.Line - context.Context.Start.Line + 1);
37+
}
3538

3639
var stringStrippedLines = string.Join(string.Empty, lines).StripStringLiterals();
3740

0 commit comments

Comments
 (0)