Joe Rivas
Joe Rivas

Reputation:

How do you manage deterministic finalization in C#?

I have a C# object with a critical resource that needs to be flushed very specific points in time making it a bad candidate to leave around for the garbage collector to take care of whenever it gets around to it, how should I handle this? Is there something like C++'s delete operator that will let me manually kill the instance of this object when needed?

Upvotes: 11

Views: 10088

Answers (6)

David
David

Reputation: 3227

This is precisely what the IDiposable interface is for. You release the critical resources in the Dispose() method, and then leave the object around for the garbage disposer to deal with deallocating the memory.

Upvotes: 2

Jeff Yates
Jeff Yates

Reputation: 62377

You are looking for IDisposable. Here is an example class that implements this.

class MyDisposableObject : IDisposable
{
   public MyDisposableObject()
   {
   }

   ~MyDisposableObject()
   {
      Dispose(false);
   }

   private bool disposed;
   private void Dispose(bool disposing)
   {
      if (!this.disposed)
      {
          if (disposing)
          {
             // Dispose of your managed resources here.
          }

          // Dispose of your unmanaged resources here.

          this.disposed = true;
       }
   }

   void IDisposable.Dispose()
   {
      Dispose(true);
      GC.SuppressFinalize(this);
   }
}

To use it, you can do something like this:

public void DoingMyThing()
{
   using (MyDisposableObject obj = new MyDisposableObject())
   {
      // Use obj here.
   }
}

The using keyword makes sure that the Dispose() method on IDisposable gets called at the end of its scope.

Upvotes: 23

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

If you are talking about a specific managed resource that you feel "MUST" be released at a specific time, you could specifcally call the Garbage Collectors' Collect method, after de-referencing the object, but there are performance considerations to be thought of, as normally the garbage collector knows when to collect items. And in general it is a bad idea.

As others are mentioning above, the IDisposable pattern is helpful for releasing unmanaged resources when needed.

NOTE: I'm going to repeat, you COULD call GC.Collect() but it is NOT a good thing, but a valid answer for the question!

Upvotes: 0

Greg D
Greg D

Reputation: 44066

The IDisposable interface exists for deterministic destruction. There's a pattern for implementing it correctly on MSDN.

In tandem, you should also consider using the using statement when your object's lifetime does not span multiple scopes.

Upvotes: 2

Nick
Nick

Reputation: 6846

The IDisposable interface was added to support deterministic destruction in C++/CLI, and you can use it from any .NET language. It's what you want.

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124696

Google for the IDisposable interface. This is the only mechanism available to you. It's tailor made if your critical resource is unmanaged. If it's a managed resource, could you be more specific about what needs to be "flushed".

Upvotes: 0

Related Questions