Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose reference #45

Open
gmlloves opened this issue Jun 20, 2023 · 1 comment
Open

Dispose reference #45

gmlloves opened this issue Jun 20, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@gmlloves
Copy link

Hello Enrico,

I re-create the charts on demand and I have some memory leaks because some references must be disposed (I am not an expert on this issue).

1.- moduleTask in ChartjsInterop:

  public async ValueTask DisposeAsync()
  {
      if (moduleTask.IsValueCreated)
      {
          await (await moduleTask.Value).DisposeAsync();
      }
  }

2.- JsModule and dotNetObjectRef in Chart.razor. :

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    try
    {
        ...
        dotNetObjectRef?.Dispose();
        dotNetObjectRef = DotNetObjectReference.Create(Config);

        JSModule ??= new ChartJsInterop(JSRuntime);
        ....
    }
    catch (ObjectDisposedException)
    {
        // Sometimes the object reference is released but JS invoke some event.
    }
}
public async ValueTask DisposeAsync()
{
    dotNetObjectRef?.Dispose();
    dotNetObjectRef = null;

    if (JSModule is not null)
    {
         await JSModule.DisposeAsync();
        JSModule = null;
    }
}

Let me know what do you think :)
Thank you,
Gaston

@erossini
Copy link
Owner

Thank you Gaston.
I'll add those suggestion in my next release.
Enrico

@erossini erossini self-assigned this Jun 25, 2023
@erossini erossini added the enhancement New feature or request label Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants