ihisham
ihisham

Reputation: 248

does the Dispose method inside a function matters?

so lately i've been working with disposable object and i was wondering would it be benific to dispose an object inside a function ? like between those 2 functions is the use of .Dispose() really matters inside a function since all objects within a function will vanish as soon as it finishes

void FOO()
{
    var x= new DisposableObject();
    //stuff
}

void FOO()
{
    using(var x= new DisposableObject())
    {
        //stuff
    }
}

Upvotes: 5

Views: 1817

Answers (4)

robaudas
robaudas

Reputation: 1638

Set a breakpoint in the Dispose() method and run these tests with debugging. TestMethod1 doesn't hit the breakpoint while TestMethod2 does.

As others have noted this is because of the way that GC works in .Net If you are going to implement the IDiisposeable interface, you probably want to put your class in a using statement or call .Dispose() so you have more predictable application behavior.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var disposable = new DisposableObject();
            disposable.DoSomething();
        }

        [TestMethod]
        public void TestMethod2()
        {
            using (var disposable = new DisposableObject())
            {
                disposable.DoSomething();
            }
        }
    }

    public class DisposableObject : IDisposable
    {
        public void Dispose()
        {
            // dispose here
        }

        public void DoSomething()
        {
            // do something here
        }
    }
}

Upvotes: 1

Jakub Lortz
Jakub Lortz

Reputation: 14894

All objects within a function will vanish as soon as it finishes

The objects will stay, the local references will vanish. The objects will 'vanish' when garbage collector runs and determines they are unreachable.

Before an unreachable object is removed from memory, it's finalizer will run (if implemented), cleaning all unmanaged resources.

The problem is that all that is not deterministic. You never know when GC will run, in some cases the finalizer will not even be executed.

You should always call Dispose method if it's possible.

If you want more details about finalizers, you should read these 2 articles: part 1, part 2.

Upvotes: 3

i3arnon
i3arnon

Reputation: 116586

Objects do not simply vanish.

The GC collects instances that are not referenced anymore. This may take time as the GC decides when to run and the instance will not be disposed until it does.

If this disposable has unmanaged resources and is implemented correctly the resources will be disposed by the finalizer which is a single thread.

If you're okay with the instance not being disposed for some time and the finalizer isn't busy.. then go ahead. But it's much better if you dispose of it as soon as possible.

Upvotes: 2

itsme86
itsme86

Reputation: 19516

You should always Dispose() an object that needs it. Even if the object is garbage collected, there could be unmanaged resources that don't get released. Calling Dispose() (or a using as in your second example) ensures that the object can properly release unmanaged resources that it needs to.

Upvotes: 7

Related Questions