serdarciplak / BlazorMonaco

Blazor component for Microsoft's Monaco Editor which powers Visual Studio Code.
https://serdarciplak.github.io/BlazorMonaco/
MIT License
432 stars 98 forks source link

Cannot access a disposed object #119

Open revenz opened 5 months ago

revenz commented 5 months ago

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)
serdarciplak commented 5 months ago

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.

trungnt2910 commented 2 months ago

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.

chrisonmoon commented 2 months ago

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

FLAMESpl commented 2 weeks ago

You can do it in two ways:

@if (rendered)
{
    <StandaloneCodeEditor />
}

@code 
{
    private bool rendered = false;

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