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.

Cases where an IDisposable may or may not be returned

See original GitHub issue

Here’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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

2reactions
JohanLarssoncommented, Feb 13, 2021

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!

1reaction
JohanLarssoncommented, Feb 14, 2021

Writing the first example like this is better for many reasons btw:

IDisposable GetSomething()
{
    if (condition)
    {
        var item = ProduceDisposableItem();
        return item;
    }
            
   return null;
   // oops, forgot to item.Dispose()
}

I get that there may be side effect reasons for writing it the other way and we can figure the case out.

Read more comments on GitHub >

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

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