BlazorMonaco icon indicating copy to clipboard operation
BlazorMonaco copied to clipboard

Cannot access a disposed object

Open revenz opened this issue 1 year ago • 4 comments

Hi, just upgraded from version 2 to 3, now im getting this error on my second instance of the editor. I open the editor in one div, then i close that div, then create a completely new instance.

I put debug statements around every place I'm calling GetValue, its none of my calls to that, so I dont think I can gracefully handle this exception.

I've tried called DiposeEditor and Dispose on the Standalone editor but no change.

Any advice?

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot access a disposed object. Object name: 'Microsoft.JSInterop.DotNetObjectReference`1[[BlazorMonaco.Editor.Editor, BlazorMonaco, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null]]'. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Microsoft.JSInterop.DotNetObjectReference`1[[BlazorMonaco.Editor.Editor, BlazorMonaco, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null]]'. at Microsoft.JSInterop.DotNetObjectReference`1[[BlazorMonaco.Editor.Editor, BlazorMonaco, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null]].ThrowIfDisposed() at Microsoft.JSInterop.DotNetObjectReference`1[[BlazorMonaco.Editor.Editor, BlazorMonaco, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null]].get_Value() at BlazorMonaco.Editor.Global.Create(IJSRuntime jsRuntime, String domElementId, StandaloneEditorConstructionOptions options, EditorOverrideServices overrideServices, DotNetObjectReference`1 dotnetObjectRef) at BlazorMonaco.Editor.StandaloneCodeEditor.OnAfterRenderAsync(Boolean firstRender) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

revenz avatar Feb 19 '24 05:02 revenz

The get_Value method mentioned in the stack trace is not the GetValue method of the editor instance. So, it's not related to your GetValue calls.

According to the stack trace, the issue is not related to the first editor instance but the new one. Looks like the new editor is already disposed when it's rendered. This sounds like a reuse of the disposed first editor instance.

If you're not disposing anything manually, this may be related to Blazor's object instance management and how Blazor reuses the instances between renders. I would suggest checking the similar values you use between the first and the second editor instance to see if Blazor may think that they're actually the same instance and reuse the first instance.

serdarciplak avatar Feb 24 '24 18:02 serdarciplak

I got the same issue on a .NET 8 Blazor Web App.

Just make sure the code editor does not get rendered in the prerender phase.

trungnt2910 avatar May 11 '24 07:05 trungnt2910

Hello - i have the same Problem.

-> Just make sure the code editor does not get rendered in the prerender phase.

How I'm gonna do that ?

THank you

chrisonmoon avatar May 19 '24 16:05 chrisonmoon

You can do it in two ways:

  • Declare rendering mode with prerendering disabled on your page eg. @rendermode="new InteractiveServerRenderMode(prerender: false)"
  • Display code editor conditionally after page is rendered for the first time:
@if (rendered)
{
    <StandaloneCodeEditor />
}

@code 
{
    private bool rendered = false;

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            rendered = true;
        }
    }
}

FLAMESpl avatar Jul 14 '24 08:07 FLAMESpl