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.

[API Proposal] Making the RenderTargetBitmap inherit IDisposable

See original GitHub issue

The 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

  1. Making the RenderTargetBitmap inherit IDisposable
public class RenderTargetBitmap : IDisposable
  1. 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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
miloushcommented, Oct 10, 2022

Funnily I was looking into this couple of days back. BitmapSource itself has unmanaged references so it would make sense to make it IDisposable, and RTB and WB can chime in and dispose their extra references.

It needs to be said that introducing IDisposable is a breaking change, because IDisposable 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 own BitmapSources as fields or properties should also implement IDisposable 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 a Dispose or Close or similarly named method without actually implementing IDisposable.

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 ObjectDisposedExceptions. 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.

1reaction
JensNordenbrocommented, Oct 30, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

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