调用C ++ / CLI在C#对象中删除 [英] Calling C++/CLI delete on C# object

查看:173
本文介绍了调用C ++ / CLI在C#对象中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从C ++ / CLI转换部分代码为C#的中间。其中一个对象都有在C ++ / CLI版本析构函数。其他一些C ++ / CLI代码调用后使用该对象上的删除。

I'm in the middle of converting some code from C++/CLI to C#. One of the objects has a destructor in the C++/CLI version. Some other C++/CLI code calls "delete" on this object after use.

我需要哪一种方法在此对象的C#版本来实现这样的删除继续朝着功能相同(IDisposable.Dispose,终结,还是其他什么东西我失踪)?

Which method do I need to implement in the C# version of this object so those "delete"s continue to function the same (IDisposable.Dispose, the finalizer, or something else that I'm missing)?

推荐答案

我会说 的IDisposable 接口是你看什么,如果你需要确定性处理资源。这通常是与非托管资源,例如需要关闭,流或数据库连接的非托管把手的情况。

I would say the IDisposable interface is what you look for if you need deterministic disposal of resources. This is usually the case with unmanaged resources, such as unmanaged handles that need to be closed, streams or database connections.

在C ++ / CLI,如果声明一个托管类型(引用类等),的IDisposable 使用析构函数语法实现的,而的Dispose()通过使用删除关键字。如果您声明一个托管类型的这样一个对象在本地(无需使用 ^ 运算符或 gcnew ),C ++ / CLI甚至会自动调用的Dispose()为你当对象超出范围。这样一来,C ++ / CLI是比C#更方便。

In C++/CLI, if you declare a managed type (ref class and the like), IDisposable is implemented using the destructor syntax, and Dispose() is called by using the delete keyword. If you declare such an object of a managed type locally (without using the ^ operator or gcnew), C++/CLI even automatically calls Dispose() for you when the object goes out of scope. In that way, C++/CLI is more convenient than C#.

您将无法调用删除使用C#时的对象,你需要调用的Dispose()手动就可以了吧。另一种方式处置的IDisposable 的对象是的 使用 块。

You won't be able to call delete on the object when using C#, you'll need to call Dispose() manually on it instead. Another way to dispose of IDisposable objects is the using block.

的终结(通过使用析构语法在C#实现)是不一样的一个C ++析,因为它是不确定性时,它会被调用。有一个终结的对象基本上都是排队,直到终结器线程决定打电话给自己的终结,因此有效地你永远不知道什么时候被调用。

The finalizer (implemented in C# by using destructor syntax) is not the same as a C++ destructor, since it is not deterministic when it will be called. Objects with a finalizer are basically queued until the finalizer thread decides to call their finalizer, so effectively you never know exactly when that is called.

有关处理非托管的最佳方法资源可能是两者的结合。看到这里推荐的方法:结果
的http:/ /msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.100).aspx

The best approach for dealing with unmanaged resources is probably a combination of the two. See here for the recommended approach:
http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.100).aspx

不过请注意,使用时的IDisposable ,即使可以确定性地处理非托管资源,管理对象仍然需要由垃圾收集器(非确定性)进行收集。

Note, however that when using IDisposable, even though you can dispose of unmanaged resources deterministically, managed objects still need to be collected by the garbage collector (non-deterministically).

我刚刚发现一篇文章,解释这个C ++ / CLI和C#之间的差异。你会觉得​​很有趣:结果
的http://博客.thinktecture.com / cnagel / 2006/04 / CCLI封边和-dispose.html

I just found an article explaining the differences of this between C++/CLI and C#. You might find it interesting:
http://weblogs.thinktecture.com/cnagel/2006/04/ccli-finalize-and-dispose.html

这篇关于调用C ++ / CLI在C#对象中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆