diff --git a/source/Common/Common.VBIDETools/Commandbar/VbeCommandBarAdapter.cs b/source/Common/Common.VBIDETools/Commandbar/VbeCommandBarAdapter.cs index 8df5198..132dfc4 100644 --- a/source/Common/Common.VBIDETools/Commandbar/VbeCommandBarAdapter.cs +++ b/source/Common/Common.VBIDETools/Commandbar/VbeCommandBarAdapter.cs @@ -113,61 +113,49 @@ protected virtual void Dispose(bool disposing) using (new BlockLogger()) { + if (disposing) + { + RemoveCommandBarButtonClickHandlers(); + } DisposeUnmanagedResources(); _disposed = true; } } - private void DisposeUnmanagedResources() + /// + /// Unadvises the COM event sinks of the command bar buttons. + /// + /// + /// Must run while the VBE is still alive (never from a finalizer). + /// If the sinks stay advised, the CLR finalizer unadvises them during process shutdown, + /// which crashes the host (access violation in combase.dll/clr.dll) and lets Office restart Access. + /// The buttons are not deleted here (issue #77: http://accunit.access-codelib.net/bugs/view.php?id=77). + /// + protected void RemoveCommandBarButtonClickHandlers() { using (new BlockLogger()) { - Marshal.ReleaseComObject(VBE); - VBE = null; - - // issue #77: (http://accunit.access-codelib.net/bugs/view.php?id=77) - return; - /* - try + while (_commandBarButtons.Count > 0) { - while (_commandBarButtons.Count > 0) + var button = _commandBarButtons.Pop(); + try { - var button = _commandBarButtons.Pop(); - try - { - Logger.Log(string.Format("Button: {0}, -ClickEventHandler", button.Button.Caption)); - button.Button.Click -= button.ClickEventHandler; - } - catch (Exception ex) - { - Logger.Log(ex); - } - try - { - Logger.Log(string.Format("Button: {0}, Delete", button.Button.Caption)); - button.Button.Delete(); - } - catch (Exception ex) - { - Logger.Log(ex); - } - try - { - button.Dispose(); - } - catch (Exception ex) - { - Logger.Log(ex); - } + button.Button.Click -= button.ClickEventHandler; + } + catch (Exception ex) + { + Logger.Log(ex); } - Logger.Log("_commandBarButtons.Clear"); - _commandBarButtons.Clear(); - } - catch (Exception ex) - { - Logger.Log(ex); } - */ + } + } + + private void DisposeUnmanagedResources() + { + using (new BlockLogger()) + { + Marshal.ReleaseComObject(VBE); + VBE = null; } } diff --git a/vbe-add-In/AccUnit.VbeAddIn/AccUnitCommandBarAdapter.cs b/vbe-add-In/AccUnit.VbeAddIn/AccUnitCommandBarAdapter.cs index 8573af8..2a9c6a0 100644 --- a/vbe-add-In/AccUnit.VbeAddIn/AccUnitCommandBarAdapter.cs +++ b/vbe-add-In/AccUnit.VbeAddIn/AccUnitCommandBarAdapter.cs @@ -111,7 +111,8 @@ protected override void Dispose(bool disposing) { if (disposing) { - // managed resources: + // unadvise COM event sinks before the command bars are deleted + RemoveCommandBarButtonClickHandlers(); } // unmanaged resources: diff --git a/vbe-add-In/AccUnit.VbeAddIn/TestStarter.cs b/vbe-add-In/AccUnit.VbeAddIn/TestStarter.cs index 126f2e6..8dbd67c 100644 --- a/vbe-add-In/AccUnit.VbeAddIn/TestStarter.cs +++ b/vbe-add-In/AccUnit.VbeAddIn/TestStarter.cs @@ -507,7 +507,19 @@ protected virtual void Dispose(bool disposing) _codeWindowRunCurrentTestButton = null; _projectWindowRunCurrentTestButton = null; - _accUnitSubMenuEvents = null; + if (_accUnitSubMenuEvents != null) + { + try + { + // unadvise COM event sink while the VBE is still alive + _accUnitSubMenuEvents.Click -= OnAccUnitSubMenuEventsClick; + } + catch (Exception ex) + { + Logger.Log(ex); + } + _accUnitSubMenuEvents = null; + } if (VbeIntegrationManager != null) {