C++/CLI, Finalize and Dispose

本文对比了C#与C++/CLI中实现资源管理的不同方式。通过具体的代码示例展示了如何使用析构函数实现IDisposable接口,并覆盖Finalize方法释放资源。同时介绍了在C++/CLI中更简洁的资源释放方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址 http://weblogs.thinktecture.com/cnagel/2006/04/ccli-finalize-and-dispose.html

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();

转载于:https://www.cnblogs.com/cxwx/archive/2010/08/07/1794762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值