Skip to content

Commit

Permalink
Merge pull request #6170 from MDoerner/TryToAvoidExceptionAtTeardown
Browse files Browse the repository at this point in the history
Try to avoid RCW exception on CommandBarButtons at shutdown
  • Loading branch information
retailcoder committed Oct 24, 2023
2 parents 415ae07 + 7ec28f3 commit c8057ec
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
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
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
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 c8057ec

Please sign in to comment.