Cases where an IDisposable may or may not be returned
See original GitHub issueHere’s a case that I don’t think is currently picked up by IDisposableAnalyzers but maybe would be a good candidate:
// Assume the caller will dispose what it receives
IDisposable GetSomething()
{
var item = ProduceDisposableItem();
if (condition) return item;
return null;
// oops, forgot to item.Dispose()
}
While that’s a simple example of the case I had in mind, a related situation is this (and where I actually had a problem in my code):
// Assume the caller will dispose what it receives
IEnumerable<IDisposable> GetStuff()
{
foreach(var item in ProduceDisposableItems())
{
if (condition) yield return item;
// should have disposed item here
}
}
As far as I can tell these cases DO require manual disposal but aren’t picked up by the analyzer. The ‘asymmetry’ made it less obvious (to me) when the code was written that a disposal was missing.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7
Top Results From Across the Web
c# - IDisposable created within a method and returned
In this case, it "looks like" you're using an IDisposable object and not calling Dispose() . Here, you're doing it by design, ...
Read more >CA2000: Dispose objects before losing scope
A local object of an IDisposable type is created, but the object is not disposed before all references to the object are out...
Read more >IDisposable: What Your Mother Never Told You About ...
IDisposable.Dispose may be called multiple times without adverse side-effects, so any real disposal code needs to be protected by a check that ...
Read more >Who Disposes Your Repository - Code Design
My rule of thumb is that the code that creates an IDisposable instance, should also call dispose on it. It is also according...
Read more >The Dispose Pattern Step by Step - Vasil Kosturski
This is not a complete beginner introduction to IDisposable and garbage collection in . ... This rule is valid in almost all of...
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
Awesome that you write issues for where we fail to warn. Most issues are about where we warn where we should not, nothing wrong with that but we very much need this type of issues. Keep 'em coming!
Writing the first example like this is better for many reasons btw:
I get that there may be side effect reasons for writing it the other way and we can figure the case out.