Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions source/AccUnit/Configuration/ErrorTrappingObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ protected virtual void Dispose(bool disposing)

try
{
if (_applicationHelper != null)
// COM calls only on deterministic dispose, never from a finalizer thread:
// if Dispose is not called, the error trapping is not restored.
if (disposing)
{
// set the error trapping back to the initial value
if (GetAccessErrorTrapping() != _initialErrorTrapping)
if (_applicationHelper != null)
{
SetErrorTrapping(_initialErrorTrapping);
// set the error trapping back to the initial value
if (GetAccessErrorTrapping() != _initialErrorTrapping)
{
SetErrorTrapping(_initialErrorTrapping);
}
}
}

if (disposing)
{
DisposeManagedResources();
}
DisposeUnmanagedResources();
Expand All @@ -115,11 +117,6 @@ void DisposeUnmanagedResources()
_hostApplication = null;
}

~AccessErrorTrappingObserver()
{
Dispose(false);
}

#endregion
}
}
21 changes: 9 additions & 12 deletions source/Common/Common.VBIDETools/Commandbar/VbeCommandBarAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
RemoveCommandBarButtonClickHandlers();
ReleaseVbe();
}
DisposeUnmanagedResources();
_disposed = true;
}
}
Expand Down Expand Up @@ -150,10 +150,17 @@ protected void RemoveCommandBarButtonClickHandlers()
}
}

private void DisposeUnmanagedResources()
/// <remarks>
/// Only called from Dispose(true): there is no finalizer, because COM objects must not be
/// touched on the finalizer thread (the STA may already be gone during process exit).
/// </remarks>
private void ReleaseVbe()
{
using (new BlockLogger())
{
if (VBE is null)
return;

Marshal.ReleaseComObject(VBE);
VBE = null;
}
Expand All @@ -165,11 +172,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

~VbeCommandBarAdapter()
{
Dispose(false);
}

#endregion

private sealed class CommandBarButtonAndClickHandler : IDisposable
Expand Down Expand Up @@ -232,11 +234,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

~CommandBarButtonAndClickHandler()
{
Dispose(false);
}

#endregion
}
}
Expand Down
20 changes: 16 additions & 4 deletions source/Common/Common.VBIDETools/HotKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AccessCodeLib.Common.VBIDETools
{
public class HotKey
public class HotKey : IDisposable
{
[Flags]
public enum ModKeys
Expand Down Expand Up @@ -50,21 +50,33 @@ public HotKey(ModKeys modKeys, uint key, IntPtr hOwner)
public ModKeys Modifiers { get; private set; }
public uint Key { get; private set; }

~HotKey()
bool _disposed;

/// <remarks>
/// UnregisterHotKey only works on the thread that registered the hot key,
/// so this must be called on deterministic dispose (not from a finalizer).
/// </remarks>
public void Dispose()
{
if (_disposed)
return;

try
{
if ((int)_hwnd != 0)
Pressed = null;
if (_hwnd != IntPtr.Zero)
{
UnregisterHotKey(_hwnd, HotKeyAtom);
_hwnd = (IntPtr)0;
_hwnd = IntPtr.Zero;
}
GlobalDeleteAtom(HotKeyAtom);
}
catch (Exception ex)
{
Logger.Log(ex);
}

_disposed = true;
}

}
Expand Down
14 changes: 13 additions & 1 deletion source/Common/Common.VBIDETools/HotKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace AccessCodeLib.Common.VBIDETools
{
public class HotKeys : List<HotKey>
public class HotKeys : List<HotKey>, IDisposable
{
private readonly IntPtr _hWnd;
public HotKeys(IntPtr hWnd)
Expand All @@ -17,5 +17,17 @@ public HotKey RegisterHotKey(HotKey.ModKeys modKeys, uint key)
Add(hotKey);
return hotKey;
}

/// <summary>
/// Unregisters all hot keys.
/// </summary>
public void Dispose()
{
foreach (var hotKey in this)
{
hotKey.Dispose();
}
Clear();
}
}
}
Loading