C++/CLI 中的垃圾收集,C# 混合代码
Garbage Collection in C++/CLI, C# Mixed Code
我正在通过引用将数组从 C# 传递到 C++/CLI 以用作输出参数。我的代码如下:
C#
ushort[] a = new ushort[1];
cppclr.method(ref a);
C++/CLI
void method(array<ushort>^% a)
{
a = gcnew array<ushort>(5);
a[0] = 1;
a[1] = 2;
a[2] = 3;
}
代码编译正常,没有产生错误。但是,我很困惑我在 C# 中创建的数组是否已被垃圾回收处理?我的困惑是,因为我在 C++/CLI 中分配了一个新内存,所以以前的引用丢失了,应该由垃圾回收处理。该程序没有显示任何内存泄漏。我是否需要以任何其他方式处理这种情况?
一切都在照顾。无论您是 运行 C# 或 C++/CLI 还是任何其他 .NET 语言,都是 相同的运行时 在幕后执行。因此,您将获得相同的 GC。
在您使用 gcnew
时,您使用了运行时的托管内存分配器。如果您在 C++/CLI 中将 new
与非托管数组一起使用,则之后必须使用 delete[]
运算符释放它。
C# 中的垃圾收集对于托管资源 是自动的,因此您不必担心它们。我在 C++ 方面经验不多,但据我所知,您正在使用 Managed C++。
The .NET Framework's garbage collector manages the allocation and
release of memory for your application. Each time you create a new
object, the common language runtime allocates memory for the object
from the managed heap. As long as address space is available in the
managed heap, the runtime continues to allocate space for new objects.
However, memory is not infinite. Eventually the garbage collector must
perform a collection in order to free some memory. The garbage
collector's optimizing engine determines the best time to perform a
collection, based upon the allocations being made. When the garbage
collector performs a collection, it checks for objects in the managed
heap that are no longer being used by the application and performs the
necessary operations to reclaim their memory.
MSDN Source Garbage Collection
您应该只关注非托管资源。(对于 C#,它们继承了 IDisposable,您应该调用 Dispose() 方法,对于 C++,调用 delete[],就像@Lucas 提到的)。
你也可以看看这个问题:
Garbage Collection Across C# and C++/CLI Objects
我正在通过引用将数组从 C# 传递到 C++/CLI 以用作输出参数。我的代码如下:
C#
ushort[] a = new ushort[1];
cppclr.method(ref a);
C++/CLI
void method(array<ushort>^% a)
{
a = gcnew array<ushort>(5);
a[0] = 1;
a[1] = 2;
a[2] = 3;
}
代码编译正常,没有产生错误。但是,我很困惑我在 C# 中创建的数组是否已被垃圾回收处理?我的困惑是,因为我在 C++/CLI 中分配了一个新内存,所以以前的引用丢失了,应该由垃圾回收处理。该程序没有显示任何内存泄漏。我是否需要以任何其他方式处理这种情况?
一切都在照顾。无论您是 运行 C# 或 C++/CLI 还是任何其他 .NET 语言,都是 相同的运行时 在幕后执行。因此,您将获得相同的 GC。
在您使用 gcnew
时,您使用了运行时的托管内存分配器。如果您在 C++/CLI 中将 new
与非托管数组一起使用,则之后必须使用 delete[]
运算符释放它。
C# 中的垃圾收集对于托管资源 是自动的,因此您不必担心它们。我在 C++ 方面经验不多,但据我所知,您正在使用 Managed C++。
The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.
MSDN Source Garbage Collection
您应该只关注非托管资源。(对于 C#,它们继承了 IDisposable,您应该调用 Dispose() 方法,对于 C++,调用 delete[],就像@Lucas 提到的)。
你也可以看看这个问题: Garbage Collection Across C# and C++/CLI Objects