Skip to content

Null out host handle in OleInterfaces when disposing #13532

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

Merged
merged 1 commit into from
May 29, 2025

Conversation

JeremyKuhne
Copy link
Member

@JeremyKuhne JeremyKuhne commented May 28, 2025

This was causing the AxHost's container to stay rooted.

Fixes #13499

Microsoft Reviewers: Open in CodeFlow

This was causing the AxHost's container to stay rooted.
@JeremyKuhne JeremyKuhne requested a review from a team as a code owner May 28, 2025 03:04
@github-actions github-actions bot added the area-DarkMode Issues relating to Dark Mode feature label May 28, 2025
@JeremyKuhne
Copy link
Member Author

JeremyKuhne commented May 28, 2025

The repro of the customer scenario in ScratchProject looks like this:

internal static class Program
{
    private const string WebBrowserClsidString = "8856f961-340a-11d0-a96b-00c04fd705a2";

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

        for (int i = 0; i < 10; i++)
        {
            Load(loadAxHost: true);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    private static void Load(bool loadAxHost)
    {
        var form = new Form1();
        if (loadAxHost)
        {
            SubAxHost ax = new(WebBrowserClsidString);
            form.Controls.Add(ax);
        }

        form.Show();
        Application.DoEvents();

        form.Close();
        Application.DoEvents();
    }

    private class SubAxHost : AxHost
    {
        public SubAxHost(string clsid) : base(clsid)
        {
        }
    }
}

Without this change, you can find Form1 in the diagnostic tools memory snapshot at the end of the program. An added finalizer will also run (after the fix) if you reregister for finalization in the Dispose method for Form1. GC.ReRegisterForFinalize(this);

@JeremyKuhne JeremyKuhne added area-COM and removed area-DarkMode Issues relating to Dark Mode feature labels May 28, 2025
Copy link

codecov bot commented May 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.59701%. Comparing base (9db17fd) to head (e30aa2b).
Report is 2 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #13532         +/-   ##
===================================================
+ Coverage   76.59556%   76.59701%   +0.00144%     
===================================================
  Files           3230        3230                 
  Lines         639148      639149          +1     
  Branches       47295       47295                 
===================================================
+ Hits          489559      489569         +10     
+ Misses        146010      146003          -7     
+ Partials        3579        3577          -2     
Flag Coverage Δ
Debug 76.59701% <100.00000%> (+0.00144%) ⬆️
integration 18.79201% <0.00000%> (+0.00343%) ⬆️
production 51.00073% <100.00000%> (+0.00331%) ⬆️
test 97.40411% <ø> (ø)
unit 48.37260% <100.00000%> (-0.02285%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JeremyKuhne JeremyKuhne merged commit 2a0c79b into dotnet:main May 29, 2025
9 checks passed
@JeremyKuhne JeremyKuhne deleted the hostrooting branch May 30, 2025 20:41
@github-actions github-actions bot locked and limited conversation to collaborators Jun 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AxHost keeping parent alive
2 participants