Does this test method introduce an undisposed of disposable thing?

Does this test method introduce an object that goes undisposed?
[/[TestMethod]
public void TestMethod1()
{
    DoSomething(new DisposableThing(...))
    ...
}

Open in new window

where DisposableThing is some class that implements IDisposable.

Is this a problem? Should disposable things never be created as parameters, because we don't hold on to them?
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
As long as the reference to an object is available to the execution of your program (in scope), then it will not be disposed (if it implements IDisposable) or otherwise garbage collected.

-saige-
0
käµfm³d 👽Commented:
As long as the reference to an object is available to the execution of your program (in scope), then it will not be disposed (if it implements IDisposable) or otherwise garbage collected.
The problem isn't the object itself. The problem is the unmanaged memory that the object holds on to. Whereas the object itself will be garbage collected whenever the GC sees fit (assuming the object is eligible for collection), the unmanaged memory that is being referenced cannot be cleaned up--the GC doesn't know about that, and cleaning up unmanaged memory is not its job.

The short of it is, yes, this code is bad. The only potential save would be if DoSomething called Dispose itself. But while that would clean up the unmanaged memory, I, as a developer, wouldn't expect a method that I pass an IDisposable thing to do my cleanup for me. I expect that if I declared/created the IDisposable thing, then I will be the one cleaning it up.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.