Skip to content

Remove COM access from finalizers and release COM objects in host lookup - #99

Merged
josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-com-cleanup
Nov 17, 2026
Merged

josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-com-cleanup

Conversation

@josef-poetzl

Copy link
Copy Markdown
Member

Follow-up to #98: fixes the three COM lifetime / cleanup problems listed there under "Not changed (found on the way)".

1. No COM access from finalizers

Finalizers run on the finalizer thread, possibly during process exit when the STA is already gone (same class of bug as the crash fixed in #98).

  • Removed the finalizers of VbeCommandBarAdapter, OfficeApplicationHelper, Connect, AddInManager and AccessErrorTrappingObserver. COM calls/releases only happen in Dispose(true), i.e. on OnDisconnection/OnBeginShutdown.
    • ~VbeCommandBarAdapter released the shared VBE RCW (ReleaseComObject(VBE)) and, via AccUnitCommandBarAdapter.Dispose(false), deleted command bars and could show UI. The undisposed adapter created in TestStarter.TryExecuteSaveMenuItem could therefore release the shared VBE RCW in the middle of a session.
    • ~AddInManager (not in the original list) released the AddIn RCW.
    • ReleaseComObject(VBE) stays in Dispose(true) (unchanged shutdown behaviour).
  • Also removed the finalizer of the inner CommandBarButtonAndClickHandler (called GC.Collect/WaitForPendingFinalizers on the finalizer thread).
  • Behaviour change: AccessErrorTrappingObserver restores the initial error trapping only on Dispose(). Internal use (AccessTestSuite) uses using; VBA code using the observer via the bridge has to call Dispose.

2. ComTools.GetOfficeComObjectByProgIDAndVBE

  • Step 1 looks up the file moniker in the ROT (CreateFileMoniker + IRunningObjectTable.GetObject) instead of Marshal.BindToMoniker, so no new Access instance can be activated; the result is checked against the VBE.
  • Candidates that do not belong to the VBE are released (steps 1-3, including the Application fallback); IMoniker, IBindCtx, IEnumMoniker and the ROT are released. The matching VBE (shared RCW) is not released.
  • GetTypeForComObject releases the GetIUnknownForObject and QueryInterface pointers.
  • ROT entries for which GetObject returns nothing are skipped (previously a NullReferenceException).

3. Hot keys

  • HotKey/HotKeys implement IDisposable (UnregisterHotKey, GlobalDeleteAtom, remove Pressed handlers) instead of unregistering in the finalizer, which cannot work from another thread.
  • Hot key registration is still disabled (issue Window title of VBE disappears in 64 bit when resizing #78), including VbeAdapter.HotKeys; a comment marks where HotKeys.Dispose() has to be called when it is re-enabled.

Tested

  • MSBuild (VS 18) Release build of vbe-add-In\AccessCodeLib.AccUnit.VbeAddIn.sln and source\AccessCodeLib.AccUnit.sln: passed.
  • Manual with real Access (DLLs replaced in access-add-in/lib, no COM registration), 4 runs, process, exit code and event log monitored:
    • VBA editor opened, Access closed via main window: exit code 0, no error events, no restarted Access process
    • test run via VBE add-in ("Run all tests"), then close: same result
    • test run with a second Access instance (other database) opened first: the add-in found the right host (ROT file moniker path), no additional Access process, both instances exited with code 0
    • in every run the command bar settings were saved on exit, i.e. Dispose(true) ran via OnDisconnection/OnBeginShutdown
  • Not tested: file not in the ROT (formerly activated a new instance), hot keys (registration disabled). Unit tests not run locally (CI).

Not changed (found on the way)

  • Connect and AddInManager both call ReleaseComObject on the same AddIn RCW.
  • AddInManager.AddInBridgeHostApplicationInitialized replaces _officeApplicationHelper without disposing the previous one.
  • ComTools: if Marshal.GetActiveObject throws (ProgID not registered in the ROT), the whole lookup is aborted before step 3.
  • OfficeApplicationHelper.GetCheckedVbProject: the string.IsNullOrEmpty condition is probably inverted.

Finalizers run on the finalizer thread, possibly during process exit
when the STA is gone (same class of bug as the crash fixed in #98).

- Remove the finalizers of VbeCommandBarAdapter, OfficeApplicationHelper,
  Connect, AddInManager and AccessErrorTrappingObserver; COM objects are
  only released in Dispose(true) (OnDisconnection/OnBeginShutdown).
  AccessErrorTrappingObserver restores the error trapping on Dispose only.
- ComTools: look up the file moniker in the ROT instead of
  Marshal.BindToMoniker (no activation of a new Access instance), check
  the result against the VBE, release non-matching candidates, ROT helper
  objects and the IUnknown/QueryInterface pointers.
- HotKey/HotKeys: unregister hot keys on Dispose instead of in the
  finalizer (registration is still disabled, issue #78).
@josef-poetzl
josef-poetzl merged commit 59fb89c into dev Nov 17, 2026
1 check passed
@josef-poetzl
josef-poetzl deleted the feature/vbe-addin-com-cleanup branch November 17, 2026 21:59
josef-poetzl added a commit that referenced this pull request Nov 17, 2026
…owup

Fix COM ownership and host lookup issues found in #99
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