[API Proposal] Making the RenderTargetBitmap inherit IDisposable
See original GitHub issueThe RenderTargetBitmap will cature the _wicSource
handler which will release slowly by GC. WPF will throw COM Exception when create RenderTargetBitmap too fast.
Making the RenderTargetBitmap inherit IDisposable. Developers can better control the release of memory.
API Proposal
- Making the RenderTargetBitmap inherit IDisposable
public class RenderTargetBitmap : IDisposable
- Add the
AsDisposable
method
public class RenderTargetBitmap
{
public IDisposable AsDisposable(){}
}
Add the AsDisposable
method can make the Analyzer happy. The Analyzer will not keep alerting that the IDisposable object may be a potential memory leak.
See https://github.com/dotnet/wpf/issues/3067#issuecomment-1065849036
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Known issue: WPF will throw COM Exception when create ...
Yes Problem description: When we use RenderTargetBitmap to take ... [API Proposal] Making the RenderTargetBitmap inherit IDisposable #7177.
Read more >c# - Implementing IDisposable in an API
My intuition suggests to make both objects A and B IDisposable so that when the user disposes of our owning object B in...
Read more >IDisposable Interface (System)
The following example demonstrates how to create a resource class that implements the IDisposable interface. #using <System.dll> #using <System.Windows.
Read more >How to use IDisposable in ASP.NET Core
Learn the different ways to dispose of objects that implement IDisposable in ASP.NET Core.
Read more >IDisposable Interface in C# | Using-Dispose Pattern
It is a breaking change to add the IDisposable interface to an existing class because pre-existing clients of the class cannot call Dispose(), ......
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
Funnily I was looking into this couple of days back.
BitmapSource
itself has unmanaged references so it would make sense to make itIDisposable
, and RTB and WB can chime in and dispose their extra references.It needs to be said that introducing
IDisposable
is a breaking change, becauseIDisposable
means “you should call dispose when you are done” and the existing code does not do that, with the worry being that unmanaged resources will leak. That is not exactly our case here, because the unmanaged resources already exist and are being taken care of during finalization. However, as @lindexi pointed out, analyzers do not know that. Another breaking change is that any classes that ownBitmapSource
s as fields or properties should also implementIDisposable
according to the guidance.So we need to make a decision whether we are OK introducing such breaking change, or whether we want to work around that. Personally at the moment I would not be opposed too much to a breaking change here. For workarounds, I do not like the
AsDisposable
idea very much, because it adds extra layer of complexity and introduces a new pattern. We could have aDispose
orClose
or similarly named method without actually implementingIDisposable
.There is also a 3rd option, which is to figure out why RTB and WB are running in these issues in the first place, for example, are they not adding enough GC pressure?
Finally, while this change looks easy in principle, there is couple of things that need to be figured out. Suddenly you can have disposed yet alive classes around and that has consequences. All the classes would need to be sprinkled with checks and
ObjectDisposedException
s. What happens to the cached bitmaps? What happens to bitmaps that are currently rendered on screen when they get disposed? Do we trigger content change on disposal? Do we need to worry about frozen clones? etc.As an api consumer I see little problems in adding using in new places or even disable analyser warnings IF need be. As long as the changes arrive in a major version like net8!