3

I am passing an array by reference from C# to C++/CLI to use as an out parameter. My code is as follows:

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;
}

The code compiles fine and produces no error. However, I am confused whether the array that I created in C# has been taken care of by the garbage collection? My confusion is that since I am assigning a new memory inside C++/CLI, the previous reference is lost and should be handled by garbage collection. The program doesn't show any memory leaks. Do I need to take care of this situation in any other way?

ubaabd
  • 435
  • 2
  • 13
  • I think that the GC will do the work, you're allocating memory using .NET (`gcnew`) and not unmanaged code. I'm not 100% sure though. – Pau C Aug 06 '16 at 09:57
  • @null I'm allocating memory using `gcnew` inside C++/CLI and that reference is kept. I'm confused about whether the memory created by C# `new` will cause any memory leak? – ubaabd Aug 06 '16 at 10:03
  • seems that I was right, since it's created with .NET the GC takes care of it. It would be a different story with CLI and unmanaged types. – Pau C Aug 06 '16 at 10:25

2 Answers2

4

Everything is being taken care of. Whether you're running C# or C++/CLI or any other .NET language for that matter, it's the same runtime that executes both under the hood. Therefore you get the same GC for both.

As you used gcnew, you used the runtime's managed memory allocator. Had you used new with an unmanaged array in C++/CLI, you'd have to free it with the delete[] operator afterwards.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0

Garbage Collection in C# is automatic for managed resources, so you should not worry about them. I don't have big experience in C++, but from what I'm seeing you are using 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

You should take care only for unmanaged resources.(For C# they inherited IDisposable and you should call Dispose() method, for C++ call delete[] like @Lucas mentioned).

You can check this question too: Garbage Collection Across C# and C++/CLI Objects

Community
  • 1
  • 1
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • OP wants a reference to a reference type, so he needs the `ref` :) Alternatively, he could have used `array^` instead of `void` as the return type to avoid the need for the `ref`/`%` parameter. – Lucas Trzesniewski Aug 06 '16 at 10:58
  • @LucasTrzesniewski thanks for the explanation like I said I'm not experienced in C++. I will remove this part. – mybirthname Aug 06 '16 at 11:06