DataGridView not dispose old elements
See original GitHub issue-
.NET Core Version: all up to 7.0 p2
-
Have you experienced this same bug with .NET Framework?: Yes
Problem description:
As I mentioned in https://github.com/dotnet/winforms/issues/6858 DataGridView
not dispose old data on DataSource
change. This is lead to 2 problems:
- All this stuff are going to finalization query because we have unnecessary finalizer here #6858.
- If we have a
ContextMenuStrip
bound to Cell/Column/Row we will have a memory leak.DataGridView
with 3 columns andContextMenuStrip
bound to 1000 rows, result of DS refresh:
Expected behavior:
Old data must be property disposed. Probably in DataGridView.RefreshColumnsAndRows()
(in DataGridView.Methods.cs
)
Minimal repro: DataGridViewLeak.zip
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Why does components.Dispose() not call DataGridView. ...
I encountered this error whenever components.dispose() is called in a windows form with DataGridView. The following exception occured in the ...
Read more >BindingList<T>, DataGridView and DataGridViewRow. ...
Hi, I have BindingList<T> that is bound to DataGridView, via BindingListView<T> like: ... Every number of items, the list is cleared, with.
Read more >Public DataGridView is Disposed By a Class and Not ...
I am copying a DataGridView on a visible UserControl to a Public DataGridView within a Class, which eventually is disposed.
Read more >DataGridViewCell.Dispose Method (System.Windows.Forms)
The Dispose method leaves the DataGridViewCell in an unusable state. After calling Dispose, you must release all references to the DataGridViewCell so the ......
Read more >Solved: My C# application hangs on closing main form at ...
The full query of 24000 records takes several minutes to close the form. It always hangs at components.Dispose() in the form Dispose method....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I need a conceptual help here 😔 What did I find out while investigating all this dispose stuff in
DataGridView
:DataGridView
does not care about the disposing of it’s components at all.DataGridViewBand
,DataGridViewCell
) all of them are end in Freachable queue. And it has drastically performance impact on apps with many constantly updating DataGridViews.My first attempt was dispose rows and cells only on databound
DataGridView
datasource change. But there were many downsides:DataGridView
not affected at all.Current attempt is to dispose only self created internal components. But it has many question too… Dispose on: clear, remove, change?
And the main one (example below is very simplified synthetic and applies to all disposable
DataGridView
components):At the moment, I see only one solution - to add a public property like
DisposeInternalComponents
with default tofalse
not to break current implementations. What do you think? P.s. during investigation I found some small bug and the ability to improve performance - I will try to create a PRs…/cc @dreddy-work @RussKie
@elachlan
Most part of the work is already done. Slightly edited my last post:
So we have three problems with dispose not to be called:
Slow down due to empty finalizers on all elements.Completed. For us this is the main problem (and I think that for all too, they just don’t realize it😁).ContextMenuStrip
- will lead to memory leak. Can be solved by user with settingContextMenuStrip
property to null before each remove/replace of this element. And yes, in some cases this is the pain.DataGridViewColumn
andDataGridViewHeaderCell
in some cases really need to be disposed. Can’t say anything aboutSite
logic 😦 Here some details.