Skip to content

Commit fcfbf44

Browse files
committed
Turned ResetComSafe on ComSafeManager into DisposeAndResetComSafe.
1 parent 72501ce commit fcfbf44

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

RetailCoder.VBE/Extension.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class _Extension : IDTExtensibility2
4242

4343
private IWindsorContainer _container;
4444
private App _app;
45-
private IComSafe _comSafe;
4645
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
4746

4847
public void OnAddInsUpdate(ref Array custom) { }
@@ -52,9 +51,6 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
5251
{
5352
try
5453
{
55-
ComSafeManager.ResetComSafe();
56-
_comSafe = ComSafeManager.GetCurrentComSafe();
57-
5854
if (Application is Microsoft.Vbe.Interop.VBE vbe1)
5955
{
6056
_ide = new VBEditor.SafeComWrappers.VBA.VBE(vbe1);
@@ -270,18 +266,6 @@ private void ShutdownAddIn()
270266
_container.Dispose();
271267
_container = null;
272268
}
273-
274-
if (_comSafe != null)
275-
{
276-
_logger.Log(LogLevel.Trace, "Disposing COM safe...");
277-
_comSafe.Dispose();
278-
_comSafe = null;
279-
_addin = null;
280-
_ide = null;
281-
}
282-
283-
_isInitialized = false;
284-
_logger.Log(LogLevel.Info, "No exceptions were thrown.");
285269
}
286270
catch (Exception e)
287271
{
@@ -291,11 +275,30 @@ private void ShutdownAddIn()
291275
}
292276
finally
293277
{
294-
_logger.Log(LogLevel.Trace, "Unregistering AppDomain handlers....");
295-
currentDomain.AssemblyResolve -= LoadFromSameFolder;
296-
currentDomain.UnhandledException -= HandlAppDomainException;
297-
_logger.Log(LogLevel.Trace, "Done. Main Shutdown completed. Toolwindows follow. Quack!");
298-
_isInitialized = false;
278+
try
279+
{
280+
_logger.Log(LogLevel.Trace, "Disposing COM safe...");
281+
ComSafeManager.DisposeAndResetComSafe();
282+
_addin = null;
283+
_ide = null;
284+
285+
_isInitialized = false;
286+
_logger.Log(LogLevel.Info, "No exceptions were thrown.");
287+
}
288+
catch (Exception e)
289+
{
290+
_logger.Error(e);
291+
_logger.Log(LogLevel.Warn, "Exception disposing the ComSafe has been swallowed.");
292+
//throw; // <<~ uncomment to crash the process
293+
}
294+
finally
295+
{
296+
_logger.Log(LogLevel.Trace, "Unregistering AppDomain handlers....");
297+
currentDomain.AssemblyResolve -= LoadFromSameFolder;
298+
currentDomain.UnhandledException -= HandlAppDomainException;
299+
_logger.Log(LogLevel.Trace, "Done. Main Shutdown completed. Toolwindows follow. Quack!");
300+
_isInitialized = false;
301+
}
299302
}
300303
}
301304
}

Rubberduck.VBEEditor/ComManagement/ComSafeManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ public static IComSafe GetCurrentComSafe()
1111
return _comSafe.Value;
1212
}
1313

14-
public static void ResetComSafe()
14+
public static void DisposeAndResetComSafe()
1515
{
16+
var oldComSafe = _comSafe.Value;
1617
_comSafe = new Lazy<IComSafe>(NewComSafe);
18+
oldComSafe.Dispose();
1719
}
1820

1921
private static IComSafe NewComSafe()

RubberduckTests/VBEditor/ComSafeManagerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ComSafeManagerTests
99
[Test()]
1010
public void ComSafeReturnedOnSecondIvokationOfGetCurrentComSafeIsTheSame()
1111
{
12-
ComSafeManager.ResetComSafe(); //Resetting to get a claen start.
12+
ComSafeManager.DisposeAndResetComSafe(); //Resetting to get a claen start.
1313

1414
var comSafe1 = ComSafeManager.GetCurrentComSafe();
1515
var comSafe2 = ComSafeManager.GetCurrentComSafe();
@@ -20,10 +20,10 @@ public void ComSafeReturnedOnSecondIvokationOfGetCurrentComSafeIsTheSame()
2020
[Test()]
2121
public void AfterCallingResetComSafeGetCurrentComSafeReturnsDifferentSafe()
2222
{
23-
ComSafeManager.ResetComSafe(); //Resetting to get a claen start.
23+
ComSafeManager.DisposeAndResetComSafe(); //Resetting to get a claen start.
2424

2525
var comSafe1 = ComSafeManager.GetCurrentComSafe();
26-
ComSafeManager.ResetComSafe();
26+
ComSafeManager.DisposeAndResetComSafe();
2727
var comSafe2 = ComSafeManager.GetCurrentComSafe();
2828

2929
Assert.AreNotSame(comSafe1, comSafe2);

0 commit comments

Comments
 (0)