LINQ Part 4 - What's behind a query expression?
Microsoft Sudoku

C++/CLI, Finalize and Dispose

C++/CLI has a great syntax for dealing with resources. The syntax of C++/CLI is simpler than C#.

To implement the interface IDisposable, with C# this syntax is needed:

// C#
public class Resource : IDisposable
{
   public void Dispose()
   {
      Console.WriteLine("release resource");
   }
}

C++/CLI is using the destructor syntax to implement the interface IDisposable:

// C++/CLI
public ref class Resource
{
public:
   ~Resource()
   {
      Console::WriteLine("release resource");
   }
};

If the Resource class allocates native resources, the Finalize method of the base class should be overridden. With C# the Finalize method is overridden using a destructor syntax:

// C#
public class Resource : IDisposable
{
   ~Resource()
   {
      Dispose();
   }


   public void Dispose()
   {
      Console.WriteLine("release resource");
   }
}

C++/CLI already uses the destructor syntax to implement IDisposable. To implement the Finalize method, a new syntax is available:

// C++/CLI
public ref class Resource
{
public:
   ~Resource()
   {
      this->!Resource();
   }
protected:
   !Resource()
   {
      Console::WriteLine("release resource");
   }

};

If the Resource class contains embedded objects that should be disposed, this pattern is needed with C#:

// C#
public class Resource : IDisposable
{
   private EmbeddedResource embedded;

   public Resource()
   {
      Console.WriteLine("allocating resource");
      embedded = new EmbeddedResource();
   }


   ~Resource()
   {
      Dispose(false);
   }

   protected virtual void Dispose(bool disposing)
   {
      Console.WriteLine("release resource");
      if (disposing)
      {
         embedded.Dispose();
      }
   }


   public void Dispose()
   {
      Dispose(true);
   }
}

This pattern is already implemented with ~Resource and !Resource:

// C++/CLI
public ref class Resource
{
private:
   EmbeddedResource^ embedded;
public:
   Resource()
   {
      embedded = gcnew EmbeddedResource();
   }
   ~Resource()
   {
      delete embedded;
      this->!Resource();
   }
protected:
   !Resource()
   {
      Console::WriteLine("release resource");
   }
};

C++/CLI also has advantages using and releasing the Resource class. With C#, the Dispose method can be invoked directly:

// C#
Resource r = new Resource();
r.Foo();
r.Dispose();

This is similar to the following C++/CLI constructs. The C++ delete gets compiled to invoking the Dispose method:

// C++/CLI
Resource^ r = gcnew Resource();
r->Foo();
delete r;

With both of these usages it was not taken into account that the Foo method may throw an exception, and thus the Dispose method is not invoked. C# has the using statement for dealing with this easily. At the end of the using scope, Dispose is invoked.

// C#
using (Resource r = new Resource())
{
   r.Foo();
}

Instead of the using statement, C++/CLI allows declaring the object locally. The object is still allocated on the managed heap. However, when the object gets out of scope, the Dispose method is invoked. This is simpler than the C# using statement.

// C++/CLI
Resource r;
r.Foo();

Christian

Comments

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

bob dawson

great

mike

came in very handy... thanks! :D

Abdul

A colleague gave me this link. Yes, very useful, especially the comparison between C# and C++.

web design sydney

It is really nice written article from you guys. I would suggest it to everyone who wants to read and learn more. There is so many helpful information’s which I couldn’t find on other websites and other similar blogs. Thank You for that.

David

Wow!

thomas sabo

Finally found what I want, I'm a beginner content inside of great help to me with you, share

Coach Outlet

Believe in yourself you can!

Hair Styles

Wow, this is the best site I’ve ever read. Thank you for sharing this.

thomas sabo uk

Thank you, creating such a wonderful site, the article is very exciting, not boring, the color is my favorite style.

zolpidem tablets

It is really nice written article from you guys. I would suggest it to everyone who wants to read and learn more. There is so many helpful information’s which I couldn’t find on other websites and other similar blogs. Thank You for that.

Discount Oakley Sunglasses

The information I found here was very useful to me. Congratulations on having such an amazing resource

Generic Viagra

Skönt att vara besöka din blogg igen, har det varit månader för mig. Väl här artikeln som jag har väntat så länge. Jag behöver den här artikeln för att slutföra mitt uppdrag i kollegiet, och den har samma tema med din artikel. Tack, bra aktie.....

Kamagra

I wonder how you got so good. This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!....

Siffredi

Thank you

Max

// C++/CLI
Resource r;
r.Foo();

Wow... And I was a "try...finally" animal. It's a nice feature :D
Thank you. Although you should clarify this is just syntax info in the title.

Cheap MBT Shoes

just wanted to let you know I enjoy reading your blog and hope you will continue writing them for a long time to come.

Ray Ban

Interesting posts, responses, and new site information. In the future, hopefully we will new pages and updated information.

fiona

swarovski crystals
swarovski outlet
swarovski store
shop swarovski
buy swarovski
Swarovski bracelet
Swarovski Crystals Bracelets
Swarovski Bangles
Swarovski Earrings
Swarovski Crystal Earrings
Swarovski Clip Earrings
Swarovski Earrings Sale
Swarovski Necklace
Swarovski Crystal Necklaces
Swarovski Colliers
Swarovski Pendant Necklaces
Swarovski Ring
Swarovski Crystals Rings
Swarovski Rings On Sale
Swarovski Rings Outlet
Air Max 2010
Air Max 95
Femme Nike Air Max 95
Air Max 90
Femme Nike Air Max 90
Air Max LTD
Air Max LTD II
Femme Nike Air Max LTD
Air Max TN
Femme Nike Air Max TN
Nike Air Max Skyline
Air Max Skyline
Femme Air Max Skyline
Air Max
Nike Air Max
Air Max pas cher
Wholesale Gucci Handbags
Wholesale D&G Handbags
Wholesale Coach Wallets
Wholesale LV Wallets
Wholesale Gucci Wallets
Wholesale Chanel Wallets
Wholesale LV Sunglasses
Wholesale Gucci Sunglasses
Wholesale Chanel Sunglasses
Wholesale D&G Sunglasses
Wholesale Oakley Sunglasses
Oakley Sunglasses
Ray.Ban Sunglasses
LV Sunglasses
Gucci Sunglasses
Chanel Sunglasses
Wholesale nfl jerseys
wholesale jerseys
wholesale football jerseys
authentic jerseys
Wholesale nfl jersey
wholesale nhl jerseys
wholesale hockey jerseys
discount nhl jerseys
Wholesale nba jerseys
wholesale basketball jerseys
discount nba jerseys
Wholesale mlb jerseys
wholesale baseball jerseys
discount mlb jerseys
Wholesale NCAA Jerseys
Wholesale Custom Jersey
Kobe Bryant Jersey
Air Jordan Shoes
Wholesale air jordans
Replica Air Jordan
Cheap Air Jordan
Packers Jerseys
Green Bay Packers Jersey

Networking solutions

Wow, Great post,Nice work, I would like to read your blog every day Thanks

Nike Pippen

Thanks for the write up! This is really some great stuff here! Wishing for a following post for the similar subject.

ザイロリック

Great tips, I would like to join your blog anyway,

SEO Services India

There are may person searching about that now they will find enough resources by your post..

Generic Viagra

Breataine agus cead a tethering a dhéanamh suas an difríocht!

Houston Home Security

Hi! I came across your site accidentally today, but am really pleased that we did! It’s not only entertaining, but in addition straightforward to utilize weighed against lots that I’ve viewed
Houton Home Security

Smart Home

Lately I came to your website and have been reading along. I thought I would leave my initial comment. Keep writing, because your posts are impressive!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)