From 2b2fcdc8c192fcab308b9dffd166d704921e96d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20P=C3=B6tzl?= Date: Tue, 17 Nov 2026 22:07:17 +0100 Subject: [PATCH] Unadvise command bar COM events on dispose to prevent crash on Access exit The click event sinks of the VBE command bar buttons were never removed (unsubscribe was disabled for issue #77). The CLR finalizer then unadvised them during process shutdown, after the VBE was gone, which crashed Access (access violation in combase.dll/clr.dll). Office crash recovery restarted Access, leaving an empty Access window. Remove the click handlers in Dispose (not from the finalizer) before the AccUnit command bars are deleted, and unsubscribe the AccUnit sub menu CommandBarEvents in TestStarter. Buttons are still not deleted. --- .../Commandbar/VbeCommandBarAdapter.cs | 74 ++++++++----------- .../AccUnitCommandBarAdapter.cs | 3 +- vbe-add-In/AccUnit.VbeAddIn/TestStarter.cs | 14 +++- 3 files changed, 46 insertions(+), 45 deletions(-) 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) {