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() is not invoked when the application is closed #149

Open
davicbaba opened this issue Jan 24, 2025 · 2 comments
Open

Dispose() is not invoked when the application is closed #149

davicbaba opened this issue Jan 24, 2025 · 2 comments

Comments

@davicbaba
Copy link

davicbaba commented Jan 24, 2025

I have encountered an issue where the Dispose() method of components is never called when the application is closed directly.

Steps to reproduce:

  • Clone the repository with the minimal code to reproduce the issue: PhotinoDisposeIssue.
  • Run the project.
  • Close the application directly.

Expected behavior:
The Dispose() method should be invoked when the application is closed.

Actual behavior:
The Dispose() method is not executed, preventing the ability to release resources, unsubscribe from events, and perform other necessary cleanup operations.

Additional resources:
The following image shows how the Dispose() method is not invoked in the mentioned project.

Alt Text

@MikeYeager
Copy link
Collaborator

@davicbaba We've not been successful with this. Dispose() is called when you navigate or refresh a page, so we know that much works. We were not "gracefully" shutting down the WebView2 control and assumed that was the culprit. We implemented a graceful shutdown and ensured that the graceful shutdown is called, but Dispose() is still not being called. We found this and other issues claiming that Dispose() may not be called on certain browsers when the browser is closed or a tab is closed. There is no discussion I can find about the WebView2 control in particular. I honestly can't think of what else Blazor might be looking for from the browser control, other than a clean shutdown. FYI, the C++ code for the clean shutdown is

void Photino::CloseWebView()
{
	if (_webviewController != nullptr)
	{
		_webviewController->Close();
		_webviewController = nullptr;
	}

	if (_webviewWindow != nullptr)
	{
		_webviewWindow->Stop();
		_webviewWindow = nullptr;
	}

	if (_webviewEnvironment != nullptr)
	{
		_webviewEnvironment = nullptr;
	}
}

@davicbaba
Copy link
Author

@MikeYeager I attempted a workaround to navigate to an intermediate page (e.g., /closing) before closing the window. I verified that when navigating to a diferent page, the lifecycle completes and Dispose() is invoked correctly.

Below is the relevant snippet from my Main() method:

private static NavigationManager _navigationManager;

[STAThread]
static void Main(string[] args)
{
    var appBuilder = PhotinoBlazorAppBuilder.CreateDefault(args);
    appBuilder.Services.AddLogging();

    // Register root component
    appBuilder.RootComponents.Add<App>("app");

    var app = appBuilder.Build();

    _navigationManager = app.Services.GetRequiredService<NavigationManager>();

    // Customize window and register window closing handler
    app.MainWindow
        .SetIconFile("favicon.ico")
        .SetTitle("Photino Hello World")
        .RegisterWindowClosingHandler((_, _) =>
        {
            _navigationManager.NavigateTo("/closing"); //Navigate to closing page to force dispose
            return false;
        });

    AppDomain.CurrentDomain.UnhandledException += (sender, error) =>
    {
        app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString());
    };

    app.Run();
}

However, when executing navigation in the closing handler, I receive the following exception:

System.InvalidOperationException: 'WebViewNavigationManager' has not been initialized.

This suggests that, at the time of window closure, the Blazor navigation infrastructure is no longer in a valid state to perform navigation.

Is there any existing option or a potential addition to Photino that would allow navigation to another page (or a similar action) during the closing process? This would force the current page to complete its lifecycle and invoke Dispose(), ensuring that resources are properly released and event subscriptions are canceled

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

No branches or pull requests

2 participants