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?
deleydAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor 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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.