question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Control can't be properly GC-collected after disposing because of it referenced via the System.Windows.Forms.Control.cachedLayoutEventArgs field

See original GitHub issue

The same bug is reproducible with .NET Framework but it seems it has no chance to be ever fixed. But the .NET Core looks like a good place for introducing some improvements in this area.

Problem description

Each instance of the System.Windows.Forms.Control type contains a private member variable of the LayoutEventArgs type - cachedLayoutEventArgs . And, the LayoutEventArgs instance typically contains a reference to some specific control. Sometimes, the cachedLayoutEventArgs field is not cleared when the child control disposing of does not affect the layout process of the parent control due to some reasons.

Reproduction

Here are the minimal reproduction-steps:

  1. Create a form with two buttons.
  2. Add the following code for the corresponding Button.Click handlers:
void btnOpenView_Click(object sender, System.EventArgs e) {
    var view = new Panel() { BackColor = Color.Red };
    view.Name = "View";
    view.Bounds = new Rectangle(100, 100, 100, 100);
    view.Parent = this;
}
void btnCloseView_Click(object sender, System.EventArgs e) {
    SuspendLayout();
    var view = this.Controls.Find("View", false)[0];
    if(view != null)
        view.Dispose();
    ResumeLayout(false);
}
  1. Press the “Open View” button - red panel appears.
  2. Press the “Close View” button - red panel disappears.

Actual behavior: The panel is still referencing via the System.Windows.Forms.Control.cachedLayoutEventArgs field at the form level. As result, it can’t be GC-collected properly and still in memory.

Expected behavior: The panel should not be referenced.

Proposal for Fix

It looks like we should use the WeakReference when caching the LayoutEventArgs. As an alternative solution, we can validate the cachedLayoutEventArgs value on child control removing and clear the problematical field.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:16 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Olina-Zhangcommented, Jul 28, 2023

Verified in the latest 8.0 SDK build: 8.0.100-rc.1.23375.11, it was fixed: there is no related panel leaks after GC-collected. image

1reaction
KlausLoeffelmanncommented, Jan 14, 2019

Sure. We should re-prioritize issues this week, anyway, but I am guessing, this will be waiting a little time longer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control.Disposing Property (System.Windows.Forms)
When this property returns true , the base Control class is in the process of disposing. After the control is disposed of, it...
Read more >
NullReferenceException is thrown in LayoutControl when ...
Exception is thrown when calling Dispose method on XtraUserControl Unhandled Exception: System.NullReferenceException: Object reference not.
Read more >
Cannot access a disposed object - How to fix?
Try checking the IsDisposed property before accessing the control. You can also check it on the FormClosing event, assuming you're using the ...
Read more >
Garbage Collection
Objects wrapping an unmanaged resource handle will always require disposal in order to free the handle. Examples include Windows Forms controls, file handles, ......
Read more >
c# - Properly disposing of, and removing references to ...
Before every new set of these UserControls are created and displayed, Dispose() is called on all the Controls currently contained in my ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found