Repository navigation
Remove COM access from finalizers and release COM objects in host lookup - #99
Merged
Merged
Conversation
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
added a commit
that referenced
this pull request
Nov 17, 2026
…owup Fix COM ownership and host lookup issues found in #99
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).
VbeCommandBarAdapter,OfficeApplicationHelper,Connect,AddInManagerandAccessErrorTrappingObserver. COM calls/releases only happen inDispose(true), i.e. onOnDisconnection/OnBeginShutdown.~VbeCommandBarAdapterreleased the shared VBE RCW (ReleaseComObject(VBE)) and, viaAccUnitCommandBarAdapter.Dispose(false), deleted command bars and could show UI. The undisposed adapter created inTestStarter.TryExecuteSaveMenuItemcould therefore release the shared VBE RCW in the middle of a session.~AddInManager(not in the original list) released theAddInRCW.ReleaseComObject(VBE)stays inDispose(true)(unchanged shutdown behaviour).CommandBarButtonAndClickHandler(calledGC.Collect/WaitForPendingFinalizerson the finalizer thread).AccessErrorTrappingObserverrestores the initial error trapping only onDispose(). Internal use (AccessTestSuite) usesusing; VBA code using the observer via the bridge has to callDispose.2.
ComTools.GetOfficeComObjectByProgIDAndVBECreateFileMoniker+IRunningObjectTable.GetObject) instead ofMarshal.BindToMoniker, so no new Access instance can be activated; the result is checked against the VBE.Applicationfallback);IMoniker,IBindCtx,IEnumMonikerand the ROT are released. The matching VBE (shared RCW) is not released.GetTypeForComObjectreleases theGetIUnknownForObjectandQueryInterfacepointers.GetObjectreturns nothing are skipped (previously aNullReferenceException).3. Hot keys
HotKey/HotKeysimplementIDisposable(UnregisterHotKey,GlobalDeleteAtom, removePressedhandlers) instead of unregistering in the finalizer, which cannot work from another thread.VbeAdapter.HotKeys; a comment marks whereHotKeys.Dispose()has to be called when it is re-enabled.Tested
vbe-add-In\AccessCodeLib.AccUnit.VbeAddIn.slnandsource\AccessCodeLib.AccUnit.sln: passed.access-add-in/lib, no COM registration), 4 runs, process, exit code and event log monitored:Dispose(true)ran viaOnDisconnection/OnBeginShutdownNot changed (found on the way)
ConnectandAddInManagerboth callReleaseComObjecton the sameAddInRCW.AddInManager.AddInBridgeHostApplicationInitializedreplaces_officeApplicationHelperwithout disposing the previous one.ComTools: ifMarshal.GetActiveObjectthrows (ProgID not registered in the ROT), the whole lookup is aborted before step 3.OfficeApplicationHelper.GetCheckedVbProject: thestring.IsNullOrEmptycondition is probably inverted.