Visual C++未管理和管理

发布于 2024-10-30 23:35:53 字数 445 浏览 1 评论 0原文

在 C++ 中创建托管和非托管 .NET 对象的实例有什么区别。也就是说,这些to语句有什么区别:

StreamWriter ^stream = gcnew StreamWriter(fileName);

vs

StreamWriter *stream = new StreamWriter(fileName);

我的假设是,如果我使用gcnew,为StreamWriter分配的内存将由垃圾收集器管理。或者,如果我使用指针(*)和new关键字,我将不得不调用delete来释放内存。

我真正的问题是:垃圾收集器是否会管理 .NET 对象内部分配的内存?例如,如果一个 .NET 对象实例化另一个对象,并且它超出了范围 - 即使我使用指针(*)和 new 关键字而不是 gcnew 和句柄(^),垃圾收集器也会管理该内存。

What are the differences between creating an instance of a .NET object in C++ that is managed vs. unmanaged. That is to say, what are the differences between these to statements:

StreamWriter ^stream = gcnew StreamWriter(fileName);

versus

StreamWriter *stream = new StreamWriter(fileName);

My assumption is that if I use gcnew, the memory allocated for StreamWriter will be managed by the garbage collector. Alternatively if I use the pointer(*) and new keyword, I will have to call delete to deallocate the memory.

My real question is: will the garbage collector manage the memory that is being allocated inside of the .NET objects? For instance if a .NET object instantiates another object, and it goes out of scope - will the garbage collector manage that memory even if I use the pointer(*) and new keyword and NOT the gcnew and handle(^).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-11-06 23:35:53

在 C++/CLI 中,您无法new .NET 对象,您将收到类似于以下错误的信息:

错误 C2750:“System::Object”:无法在引用类型上使用“new”;使用“gcnew”代替

旧版 C++ 托管扩展中允许对 .NET 对象使用 new/clr:oldsyntax 编译器标志)。 “托管 C++”现已被弃用,因为它太糟糕了。它已被 C++/CLI 取代,后者引入了 ^gcnew

在 C++/CLI 中,对于托管类型,您必须使用 gcnew (和 ^ 句柄),并且必须使用 new (和 *< /code> 指针)用于本机类型。如果您使用 new 在本机堆上创建对象,则您有责任在使用完它们后销毁它们。

理想情况下,您应该使用智能指针(例如 std::shared_ptr 或 std::unique_ptr)来管理本机堆上的对象。但是,由于您不能将本机智能指针作为 ref 类的字段,因此这并不完全简单。最简单和最通用的方法可能是编写您自己的智能指针包装器引用类来正确实现IDisposable

In C++/CLI, you can't new a .NET object, you'll get something similar to the following error:

error C2750: 'System::Object' : cannot use 'new' on the reference type; use 'gcnew' instead

Usage of new for .NET objects is allowed in the older Managed Extensions for C++ (/clr:oldsyntax compiler flag). "Managed C++" is now deprecated because it is horrible. It has been superceded by C++/CLI, which introduced the ^ and gcnew.

In C++/CLI, you must use gcnew (and ^ handles) for managed types and you must use new (and * pointers) for native types. If you do create objects on the native heap using new, it is your responsibility to destroy them when you are done with them.

Ideally you should use a smart pointer (like std::shared_ptr or std::unique_ptr) to manage the object on the native heap. However, since you can't have a native smart pointer as a field of a ref class, this isn't entirely straightforward. The simplest and most generic approach would probably be to write your own smart pointer wrapper ref class that correctly implements IDisposable.

埋情葬爱 2024-11-06 23:35:53

当您使用gcnew创建对象时,它会绑定到垃圾收集器,并且垃圾收集器将负责销毁它。

如果您使用new,它将不会绑定到垃圾收集器,并且您将负责删除该对象。

只是为了澄清:
如果您有一个托管 C# 对象,其中包含一个非托管对象,则垃圾收集器不会删除该非托管对象。它只会在删除托管对象之前调用其析构函数(如果存在)。您应该在析构函数中编写自己的代码来删除您创建的非托管对象。

When you create an object with gcnew it is binded to the garbage collector, and the garbage collector will be responsible for destroying it.

If you use new it won't be binded to the garbage collector, and it will be your responsibility to delete the object.

Just to clarify:
If you have a managed C# object that contains within it an unmanaged object, the garbage collector will not delete the unmanaged object. It will just call the managed object's destructor (if it exists) before it is deleted. You should write your own code in the destructor to delete the unmanaged objects you created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文