dotnet / dotnet/winforms

DataGridView memory leakage when removing rows with RemoveAt

Open
#13,593 7 comments 0 reactions 1 assignee Claimed by @KlausLoeffelmann View on GitHub
area-Binding
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`:

![Initial Snapshot](https://github.com/user-attachments/assets/4b0926f4-a223-400f-94d9-b09772980195)

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

![After Hide/Show](https://github.com/user-attachments/assets/b3a85e33-ec57-4e6f-977b-204b9f02d8d6)

![Image](https://github.com/user-attachments/assets/3bcaeae4-d29b-4ce7-9d49-31532d89243d)

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

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.