Skip to content

Fix Access crash on exit caused by VBE add-in command bar events - #98

Merged
josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-orphan-access-instance
Nov 17, 2026
Merged

josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-orphan-access-instance

Conversation

@josef-poetzl

Copy link
Copy Markdown
Member

Problem

With the AccUnit VBE add-in loaded and the VBA editor opened, closing Access crashed the process on every exit (5 of 5 runs). Sometimes Office crash recovery then restarted Access, which showed up as a new, empty Access window after closing.

Event log: access violation in combase.dll, followed by .NET Runtime event 1023 (exit code 80131506) and an access violation in clr.dll.

Cause (from a full crash dump)

  • Finalizer thread: ComEventsInfo.Finalize → ComEventsSink.RemoveAll → ComEventsSink.Unadvise → COM call marshalled into the main STA thread.
  • Main thread: already in process exit (CorExitProcess); dispatching the incoming Unadvise call crashes in combase, because the VBE command bar objects are gone.
  • The finalization queue contained 17 ComEventsInfo and 17 VbeCommandBarAdapter+CommandBarButtonAndClickHandler: the Click handlers of the command bar buttons were never removed. VbeCommandBarAdapter.DisposeUnmanagedResources returned early (issue #77) and the unsubscribe code was commented out. TestStarter.Dispose also only set _accUnitSubMenuEvents to null without removing its Click handler.

Change

  • VbeCommandBarAdapter: new RemoveCommandBarButtonClickHandlers(), called from Dispose(true) only (never from the finalizer). Buttons are still not deleted (presumed reason for issue Test Explorer #77). Dead code removed.
  • AccUnitCommandBarAdapter: removes the click handlers before deleting the AccUnit command bar and sub menu.
  • TestStarter: removes OnAccUnitSubMenuEventsClick from _accUnitSubMenuEvents.Click on dispose.

Tested

  • MSBuild (VS 18) Release build of AccessCodeLib.AccUnit.VbeAddIn.sln: passed.
  • Manual with real Access (DLL replaced in access-add-in/lib, no COM registration), process and event log monitored:
    • before: 5 of 5 runs crashed (VBA editor opened, with or without test run); without the VBE add-in loaded: no crash
    • after: 3 of 3 runs clean (test run via VBE add-in; VBA editor only; all other VBE add-ins enabled: MZ-Tools, VBE_Extras, vbWatchdog)
  • Unit tests not run locally (CI).

Not changed (found on the way)

  • ComTools.GetOfficeComObjectByProgIDAndVBE: Marshal.BindToMoniker(fileName) can start a new Access instance if the file is not in the ROT, and its result is not checked against the VBE; ROT/GetActiveObject candidates and the pointers from GetIUnknownForObject/QueryInterface in GetTypeForComObject are not released.
  • Finalizers touching COM: ~VbeCommandBarAdapter (ReleaseComObject(VBE) on a shared RCW), ~OfficeApplicationHelper, ~Connect, ~AccessErrorTrappingObserver (calls GetOption/SetOption).
  • TestStarter: registered hotkeys are not unregistered on dispose.

… 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.
@josef-poetzl
josef-poetzl merged commit 52adaba into dev Nov 17, 2026
1 check passed
@josef-poetzl
josef-poetzl deleted the feature/vbe-addin-orphan-access-instance branch November 17, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant