-
Notifications
You must be signed in to change notification settings - Fork 971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Control can't be properly GC-collected after disposing because of it referenced via the System.Windows.Forms.Control.cachedLayoutEventArgs field #165
Comments
Could you be more clear about these scenarios? What is not triggering when the child control disposing does not affect the layout process of the parent control? |
Hi, @zsd4yr
From my experience, there are two cases when this issue is 100% reproducible:
Here are the steps for last case reproduction:
Also, there are some scenarios when this issue is reproducible from time to time:
|
I'll take a look! |
@KlausLoeffelmann @DmitryGaravsky
I think this won't work, if nobody holds a reference to the LayoutEventArgs it will be collected immediately, making the cache useless. What you want is a ConditionalWeakTable where the key is the Control and the value the LayoutEventArgs (ConditionalWeakReference does not exist unfortunately, and the framework API required to build one is internal). |
Yes, you're absolutely correct, but I proposed not the exact replacement of |
@KlausLoeffelmann, we have been very busy these last few weeks! When you get a chance, could you update this thread with any progress? |
Sure. We should re-prioritize issues this week, anyway, but I am guessing, this will be waiting a little time longer. |
Could we refactor the weltkante also raised an issue for the |
@Olina-Zhang can your team verify this issue? |
While there isn't a Note there are GC issues around dependent handles that can cause memory leaks if used incorrectly, since their original design was for static caches (ConditionalWeakTable). See this comment for a summary or the dotnet/runtime#12255 for the main issue.
I don't see any immediate problem, could work |
See #9393 for the PR, I would love some feedback. |
Hi @elachlan @DmitryGaravsky, we try to verify this issue on WinDbg, no matter before and after disposing Panel, we all get 1 object using |
@Amy-Li03 maybe try: #165 (comment) |
Not clear if its been considered but for WeakReference to be cleared you may have to wait (or induce) a GC. This wasn't relevant before, but with the proposed fix you have to consider this.
|
Verified with .NET SDK 8.0.100-preview.7.23376.3 test pass build, this issue was fixed, test result same as above. |
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 theLayoutEventArgs
type - cachedLayoutEventArgs . And, theLayoutEventArgs
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:
Button.Click
handlers: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 theLayoutEventArgs
.As an alternative solution, we can validate the
cachedLayoutEventArgs
value on child control removing and clear the problematical field.The text was updated successfully, but these errors were encountered: