Skip to content

Dispose IServiceScope when window is destroyed to prevent memory leaks #30196

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 25, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR implements proper disposal of IServiceScope instances when windows are destroyed, addressing a memory leak where window-scoped services were never being disposed.

Problem

Currently, .NET MAUI creates window-scoped service scopes in MauiContextExtensions.MakeWindowScope() but never disposes them when the associated window is closed or destroyed. This leads to memory leaks for services registered with scoped lifetime.

The issue was marked with a TODO comment in the code:

// TODO: We need to dispose of these services once the window closes
scope = mauiContext.Services.CreateScope();

Solution

  1. Added scope tracking to MauiContext: Added internal methods SetWindowScope() and DisposeWindowScope() to track the window-scoped service scope
  2. Updated MakeWindowScope: Modified to store the created scope in the MauiContext for later disposal
  3. Added disposal logic: Updated Window.Destroying() to dispose the scope before disconnecting the handler
  4. Maintained API compatibility: Used internal methods to avoid breaking public API surface

Testing

Added comprehensive tests to verify:

  • Service scopes are properly disposed when windows are destroyed
  • Null safety when no scope is present
  • Full integration with the window creation flow
  • Scoped service lifetime behavior

All existing tests continue to pass (53 window-related tests ✅).

Example Usage

The fix is transparent to users - no code changes required. Services registered with scoped lifetime will now be properly disposed when windows are destroyed:

// Services registered like this will now be properly disposed
services.AddScoped<MyWindowScopedService>();

Fixes #8538.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits June 25, 2025 12:17
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] IServiceScope should get disposed when the associated window is destroyed or closed Dispose IServiceScope when window is destroyed to prevent memory leaks Jun 25, 2025
@Copilot Copilot AI requested a review from PureWeen June 25, 2025 12:19
Copilot finished work on behalf of PureWeen June 25, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IServiceScope should get disposed when the associated window is destroyed or closed
2 participants