C++/CLI and IDisposable
Distributed Applications - Today and in the Future

Dispose and Finalize with C++/CLI, C# and VB

Depending on the .NET language more or less of implementing resource management with the interface IDisposable and the finalizer is hidden. Interestingly, with VB nothing is hidden - however all the code is created automatically with Visual Studio 2005 as soon as the interface IDisposable is implemented.

The syntax implementing the Dispose method of the IDisposable interface and overriding the Finalize method of the Object class is shown with this table:

IL C++/CLI C# VB
Dispose ~ClassName Dispose Dispose
Finalize !ClassName ~ClassName Finalize

Visual Basic does not hide Dispose and Finalize. The automatically created code (with Visual Studio 2005) also includes the Dispose(bool) pattern:

Public Class Resource
    Implements IDisposable

    Private disposed As Boolean = False

    ' IDisposable
    Private Overloads Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposed Then
            If disposing Then
                ' TODO: put code to dispose managed resources
            End If

            ' TODO: put code to free unmanaged resources here
        End If
        Me.disposed = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Protected Overrides Sub Finalize()
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(False)
        MyBase.Finalize()
    End Sub
#End Region

End Class

Implementing a similar functionality with C#, the destructor syntax is used to override the Finalize method. The IDisposable interface and the Dispose pattern must be implemented traditionally.

class Resource : IDisposable
{
  private bool disposed = false;

  ~Resource()
  {
    Dispose(false);
  }

  protected void Dispose(bool disposing)
  {
    if (disposing)
    {
      // dispose managed resources
    }
    // dispose unmanaged resources

    disposed = true;
  }

  #region IDisposable Members
 
public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }
 
#endregion

  public void Foo()
  {
    if (disposed)
      throw new ObjectDisposedException("Object already disposed");
    //...

  }
}

C++/CLI not only hides the implementation of the Finalize method, but also the implementation of the IDisposable interface with the Dispose method. Contrary to C# where the destructor is used to implement the finalizer, here the destructor is used to implement IDisposable. Calling this method is done with the delete operator. The destructor syntax automatically includes code for GC.SuppressFinalize(this).

To override the Finalize method, a new syntax is used: !Class().

The Dispose(bool) pattern still must be done.

ref class Resource
{
private:
  bool disposed;
public:
  Resource()
  {
    disposed = false;
  }

  ~Resource() // IDisposable
  {
    Dispose(true);
  }

protected:
  !Resource() // Finalize
  {
    Dispose(false);
  }

  void Dispose(bool disposing)
  {
    if (disposing)
    {
      // dispose managed resources
    }
    // dispose unmanaged resources

    disposed = true;
  }
};

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 04/27/2005 03:15:00 PM

Andrew Renner

After reading a bunch of different articles and implementations I have finally come to terms with the...

art paintings

I really enjoyed the great benefit of your article concerning the matter. I have bookmarked it and will come back for more useful information.

Laptop Battery

After reading a bunch of different articles and implementations I have finally come to terms with the...

christian louboutin

No country , however rich , can afford the waste of its human resources

moncler jackets

oh so funny!

air jordan

oh it's so great´╝ü

Link building

Thanks for this article. It's just what I was searching for. I am always interested in this subject. Will bookmark it.

ugg boots

We must take change by the hand or rest assuredly, change will take us by the throat

Oil Press

great one
i just wonder why i come here so late...

how to win the lottery

And it`s better?? If I Dispose and Finalize with C++/CLI, C# and VB ??

Anti Aging Diet

thanks for share these tips ... great ! Regards

Compaq Presario Batteries


I would like to thank you for sharing this great information with us. I am really glad to learn about this because it helps me to increase my knowledge.

how to build muscle

with all the .NET language more or less of implementing resource management with the interface i don`t think that`s a good idea!

air jordan shoes

i would like to thank you for sharing this great information with us. I am really glad to learn about this because it helps me to increase my knowledge.

Herve leger

No country , however rich , can afford the waste of its human resources ........

Kobe VI

You need to justification approximately values, and provides a fabulous reasoned (and reason-responsive) barricade about values as being a definite increased class of feeling worth specialized attention, We're desirous to carry out. Document most certainly federal all the your life for the way about values; the things I have to watch is mostly a reasoned place to take values truly as a means getting to point, and not just, tell you, only just as a means most people consolation his or her self and additionally a single another (a quality feature which i implement receive seriously). Still you mustn't believe others to make sure you go with any defence about values in the form of road to point any time located at whatever phase you will tempt safeguard the delicate dispensation you can be apparently wishing to warrant. Prior to when you tempt values the moment justification comes with you will subsidized perfectly into a nearby, ponder when you will want to depart from justification the moment justification is normally in your corner.

Cheap nfl jerseys wholesale

great one
i just wonder why i come here so late...

Cheap NFL Jerseys

looked down at Kasi association Korea, he stretched out a hand that only , grabbed the Kasi association Korea to walk away. . . - Fen Leer got those blacksmith to Luo Well satisfied, they refer to the Ron's drawings, no less need to build out

uggs clearance uk

great one
i just wonder why i come here so late...

supra vaider

let's join our hands together to stop this kind of wrong doings. It may risk lives in the future if we just let them continue.

cheap authentic nfl jerseys

Great!I am longing for your other excellent articles.

coach outlet online

I agree you the up.As it give me the same some feeling!

Oil Painting

Hey just a thought, you would probably get more readers if you interviewed controversial people for your blog.

mulberry alexa outlet

I like you on facebook and follow through google reader!

The comments to this entry are closed.