Skip to content

Commit 12e8dbb

Browse files
authored
[Menu] More fixes for dispose error (#4258)
* Fix #4248 * makes JSObject references nullable and set to null in Dispose * - Add _disposed and check it at the required places - Undo nullable for js object references * Remove null assigments in DisposeAsync * - Add another dispose check
1 parent db185f1 commit 12e8dbb

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Core/Components/Menu/FluentMenu.razor.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public partial class FluentMenu : FluentComponentBase, IAsyncDisposable
2525
private IMenuService? _menuService = null;
2626
private IJSObjectReference _jsModule = default!;
2727
private IJSObjectReference _anchoredRegionModule = default!;
28+
private bool _disposed;
2829

2930
private (int top, int right, int bottom, int left) _stylePositions;
3031

@@ -223,7 +224,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
223224

224225
if (Trigger != MouseButton.None)
225226
{
226-
if (Anchor is not null)
227+
if (!_disposed && Anchor is not null)
227228
{
228229
// Add LeftClick event
229230
if (Trigger == MouseButton.Left)
@@ -240,14 +241,14 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
240241
}
241242
}
242243

243-
if (_jsModule is not null && _anchoredRegionModule is not null)
244+
if (!_disposed)
244245
{
245246
await _jsModule.InvokeVoidAsync("initialize", Anchor, Id, Open, _anchoredRegionModule, _dotNetHelper);
246247
}
247248
}
248249
else
249250
{
250-
if (_jsModule is not null && _anchoredRegionModule is not null && _reinitializeEventListeners)
251+
if (!_disposed && _reinitializeEventListeners)
251252
{
252253
// If the menu was closed, remove its set event listeners. If it opened (ie if the menu starts out closed),
253254
// we should set them now.
@@ -385,6 +386,10 @@ internal async Task NotifyCheckedChangedAsync(FluentMenuItem fluentMenuItem)
385386

386387
internal async Task<bool> IsCheckedAsync(FluentMenuItem item)
387388
{
389+
if (_jsModule is null)
390+
{
391+
return false;
392+
}
388393
return await _jsModule.InvokeAsync<bool>("isChecked", item.Id);
389394
}
390395

@@ -393,6 +398,12 @@ internal async Task<bool> IsCheckedAsync(FluentMenuItem item)
393398
/// </summary>
394399
public async ValueTask DisposeAsync()
395400
{
401+
if (_disposed)
402+
{
403+
return;
404+
}
405+
406+
_disposed = true;
396407
_dotNetHelper?.Dispose();
397408

398409
try
@@ -414,5 +425,9 @@ public async ValueTask DisposeAsync()
414425
// The JSRuntime side may routinely be gone already if the reason we're disposing is that
415426
// the client disconnected. This is not an error.
416427
}
428+
finally
429+
{
430+
GC.SuppressFinalize(this);
431+
}
417432
}
418433
}

0 commit comments

Comments
 (0)