DataGridView memory leakage when removing rows with RemoveAt
- Dominant language
- C#
- Stars
- 4.9k
- Forks
- 1.1k
- Avg merge
- 20h 23m
- Merged PRs (30d)
- 103
Description
### .NET version
.NET 9.0
### Did it work in .NET Framework?
Yes
### Did it work in any of the earlier releases of .NET Core or .NET 5+?
_No response_
### Issue description
I have a specific usage pattern with a `DataGridView` where I **pre-create a set of `DataGridViewRow` instances** and then dynamically **add or remove** these rows from an existing `DataGridView` at runtime.
Here’s how I currently manage rows:
- **`Add`** – I use this to add pre-created `DataGridViewRow` objects to the `DataGridView`. This works as expected.
- **`Clear`** – I use this to remove all rows at once. This also works correctly and does not seem to cause any issues.
- **`RemoveAt`** – I frequently use this to remove rows at specific indices. However, I've noticed that repeated use of `RemoveAt` appears to cause **memory leaks** over time.
This suggests that `RemoveAt` may not be fully disposing of internal row resources or references.
### Steps to reproduce
I’ve created a minimal reproducible codebase that demonstrates the issue:
📁 **[WinFormsApp1.zip](https://github.com/user-attachments/files/20735096/WinFormsApp1.zip)**
I'm observing unexpected memory behavior after adding and removing `DataGridViewRow` objects repeatedly. Specifically, memory snapshots reveal a buildup of tooltip-related objects even after rows are removed.
#### 📸 Memory Snapshot Before `Hide/Show`:

#### 📸 Memory Snapshot After One `Hide/Show` Cycle:


As shown, there's a significant increase in:
- `ConditionalWeakTable+Entry>[]`
- `WeakReference`
These objects appear to accumulate with each hide/show cycle, and may not be getting released properly, potentially indicating a **memory leak related to tooltip handling in `DataGridViewRow`**.
I'm looking for guidance on:
- Whether this is a known issue with `DataGridView` tooltip internals.
- Recommended practices to avoid or mitigate this kind of memory retention when programmatically adding/removing large numbers of rows.
Contributor guide
Assessment
This issue has not been assessed yet.