dotnet / dotnet/winforms

Reopen issue 13499: AxHost keeping parent alive

Open
#13,659 3 comments 0 reactions 1 assignee Claimed by @JeremyKuhne View on GitHub
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

.NET 8

### Did it work in .NET Framework?

Yes

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

Haven't tried on earlier, but also broken on .NET 9

### Issue description

The container of an AxHost never gets garbage collected. We originally discovered this while investigating a GDI Object leak, which is a manifestation of the container never getting collected.

The "solution" in Issue 13499 did not work.

### Steps to reproduce

The attached projects demonstrate the issue by tracking when the container is constructed, disposed and finalized. The three projects are:
ActiveXControl_MFC - an MFC project that builds an ocx; the post-build step registers it (requires Visual Studio 2022 run as admin) and creates an interop assembly
AxHostContainer - a .NET 8 project that contains the AxHost
AxHostContainer_Framework48 - a .NET Framework 4.8 project that contains the AxHost
The AxHostContainer projects are identical, except for the targeted .NET platform.

Running AxHostContainer apps:
The AxHostContainer apps effectively execute four cases, two on .NET 8 and the same two repeated on .NET Framework 4.8. Each of the four cases open and close a Form multiple times.
```

Without AxHost With AxHost
.NET 8 + x
.NET FW + +
```

Only the .NET 8 With AxHost case exhibits the bad behavior where the finalizers fail to execute.

[AxHostLeak.zip](https://github.com/user-attachments/files/20367779/AxHostLeak.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.