RAII在C ++ / CLI中 [英] RAII in C++/CLI

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

问题描述

我习惯于C ++ RAII设施,我想使用RAII正确的方式与C ++ / CLI中的托管代码。 草本 Sutter Microsoft 都告诉我这是最好的做法。

I'm used to the C++ RAII facilities, and I want to use RAII the right way with managed code in C++/CLI. Herb Sutter and Microsoft both tell me this is the best practice.

我有这样的: p>

I have something like this:

ref struct Managed
{
    // No default constructor
    Managed( /*...*/ ) { /*...*/ }
    ~Managed() { /* Important non-managed resource release here */ }
    // ...
};

ref struct UsesManaged
{
    Managed^         m_;
    array<Managed^>^ a_;

    UsesManaged( Managed^ m, array<Managed^>^ a ) : m_(m), a_(a) {}
    // ...
};

ref struct Creator
{
    Managed^         m_;
    array<Managed^>^ a_;
    UsesManaged^     u_;

    Creator()
    {
        // Must allocate dynamically here, not in initializer list
        // because in my real code, I use "this" here for a callback.
        m_      = gcnew Managed( /*...*/ );
        a_      = gcnew array<Managed^>( 2 );
        a_[ 0 ] = gcnew Managed( /*...*/ );
        a_[ 1 ] = gcnew Managed( /*...*/ );
        u_      = gcnew UsesManaged( m_, a_ );
    }
};

我想要(1)自动资源销毁,所以我不必删除每个gcnew'ed对象手动,特别是面对例外; (2)安全而清晰地共享对象的能力(传递std :: auto_ptr等不符合条件);和(3)能够使我的类由VB或C#消费,并且当对象超出范围时(例如,由于异常),清除自动运行。

I want (1) automatic resource destruction so I don't have to delete every gcnew'ed object manually, particularly in the face of exceptions; (2) the ability to share objects safely and clearly (passing around std::auto_ptr and the like doesn't qualify); and (3) the ability to have my class consumed by VB or C# and have the cleanup automatically run when the object goes out of scope (e.g., due to an exception).

在标准C ++中,我将使用std :: shared_ptr和std :: vector或类似的工具来自动化RAII。在这里,我可以使用STL / CLI的向量,但没有shared_ptr等效。我看到的唯一相关的C ++ / CLI智能指针是稀疏记录的msclr :: auto_handle ,类似于std :: auto_ptr,包括所有权转移语义,它们与向量不兼容,尽管它们在数组中工作正常。

In standard C++ I'd use std::shared_ptr and std::vector or similar facilities to automate RAII. Here, I could use STL/CLI's vector, but there is no shared_ptr equivalent. The only relevant C++/CLI smart pointer I see is the sparsely documented msclr::auto_handle, which is akin to std::auto_ptr, including transfer-of-ownership semantics, which are not compatible with vectors, though they'd work alright in an array.

什么是正确的C ++ / CLI方式来实现我的三个目标? (注意,我的主要C ++ / CLI类,上面的Creator将被VB / C#使用。)

What's the proper C++/CLI way to achieve my three goals? (Note also, my main C++/CLI class, Creator in the above, will be consumed by VB/C#.)

[更新:添加了Herb Sutter和MS在顶部和添加的目标3(消费VB / C#)。]

[Updates: Added links to Herb Sutter and MS at the top and added goal 3 (consumption by VB/C#).]

推荐答案

我对我的问题的正确答案 Billy ONeal 对其他人的回答的评论:

I take the correct answer to my question to be Billy ONeal's comment on someone else's answer:

啊 - 你不能这样做。没有
的方式强制垃圾回收器
销毁一个对象。请参阅
blogs.msdn.com/b/ oldnewthing / archive / 2010/08/09 / 10047586.aspx
正确的方法是要求
显式关闭调用,并将
调用在终结器。如果对于
有一些原因,客户端代码不会
清理,GC将(最终)
清理你(在程序
终止,如果没有别的),但是你
不能编写依赖于
的代码。 - Billy ONeal Sep 10 '10 at
16:02

Ah -- you can't do that. There is no way to force the garbage collector to destroy an object. See blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx The right way to do that is to require an explicit close call, and to put a call to that in a finalizer. If for some reason the client code doesn't clean up, the GC will (eventually) clean it up for you (at program termination if nothing else), but you cannot write code that depends on that. – Billy ONeal Sep 10 '10 at 16:02

除了他给出的链接,另见此处此处

Besides the link he gives, see also here and here.

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

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