Skip to content

Commit

Permalink
Try to avoid RCW exception at shutdown
Browse files Browse the repository at this point in the history
This wraps the calls to set and get the enabled state, the visibility and the tag of a CommanBarButton in a check whether it has already been released an logs any attempt the access these methods after the release as a warning.

This should lead to a clean exit. However, it only fixes the symptoms of apparently too late CanExecute checks. Identifying these is made difficult by the fact that setting the enabled state is dispatched to the UI thread, which cuts off the stack trace.
  • Loading branch information
MDoerner committed Oct 21, 2023
1 parent 877d0f6 commit 7ec28f3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public async Task EvaluateCanExecuteAsync(RubberduckParserState state, Cancellat
}
catch (Exception exception)
{
value.IsEnabled = false;
Logger.Error(exception, "Could not evaluate availability of commmand menu item {0}.", value.Tag ?? "{Unknown}");
value.IsEnabled = false;
}
});
break;
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.VBEEditor/SafeComWrappers/SafeComWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void Release(bool final = false)
}
}

private bool HasBeenReleased => _rcwReferenceCount <= 0;
protected bool HasBeenReleased => _rcwReferenceCount <= 0;

public bool IsWrappingNullReference => Target == null;
object INullObjectWrapper.Target => Target;
Expand Down
81 changes: 70 additions & 11 deletions Rubberduck.VBEditor.VBA/SafeComWrappers/Office/CommandBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public sealed class CommandBarButton : SafeEventedComWrapper<MSO.CommandBarButto

public const bool AddCommandBarControlsTemporarily = false;

public CommandBarButton(MSO.CommandBarButton target, bool rewrapping = false)
public CommandBarButton(MSO.CommandBarButton target, bool rewrapping = false)
: base(target, rewrapping)
{
_control = new CommandBarControl(target, true);
}

private MSO.CommandBarButton Button => Target;

public bool IsBuiltInFace
{
get => !IsWrappingNullReference && Button.BuiltInFace;
Expand All @@ -35,7 +35,7 @@ public bool IsBuiltInFace
}
}

public int FaceId
public int FaceId
{
get => IsWrappingNullReference ? 0 : Button.FaceId;
set
Expand Down Expand Up @@ -183,13 +183,31 @@ public string Caption
public string DescriptionText
{
get => _control.DescriptionText;
set => _control.DescriptionText=value;
set => _control.DescriptionText = value;
}

public bool IsEnabled
{
get => _control.IsEnabled;
set=> _control.IsEnabled = value;
get
{
if (HasBeenReleased)
{
_logger.Warn($"Getting IsEnabled of already release CommandBarButton.");
return false;
}
else
{
return _control.IsEnabled;
}
}
set
{
if (!HasBeenReleased) {
_control.IsEnabled = value;
} else {
_logger.Warn($"Setting IsEnabled on already release CommandBarButton.");
}
}
}

public int Height
Expand Down Expand Up @@ -226,8 +244,28 @@ public int Priority

public string Tag
{
get => _control.Tag;
set => _control.Tag = value;
get
{
if (HasBeenReleased)
{
_logger.Warn($"Getting Tag of already release CommandBarButton.");
return null;
} else
{
return _control.Tag;
}
}
set
{
if (!HasBeenReleased)
{
_control.Tag = value;
}
else
{
_logger.Warn($"Setting Tag on already release CommandBarButton.");
}
}
}

public string TooltipText
Expand All @@ -242,8 +280,29 @@ public string TooltipText

public bool IsVisible
{
get => _control.IsVisible;
set => _control.IsVisible = value;
get
{
if (HasBeenReleased)
{
_logger.Warn($"Getting IsVisible of already release CommandBarButton.");
return false;
}
else
{
return _control.IsVisible;
}
}
set
{
if (!HasBeenReleased)
{
_control.IsVisible = value;
}
else
{
_logger.Warn($"Setting IsVisible on already release CommandBarButton.");
}
}
}

public int Width
Expand Down

0 comments on commit 7ec28f3

Please sign in to comment.