I’m currently looking into using C++/CLI to bridge the gap between managed C# and native, unmanaged C++ code. One particular issue I’m looking to resolve, is the conversion of data types that are different in C# and C++.
While reading up on the use of such a bridging approach and the performance implications involved, I wondered how Garbage Collection would work. Specifically, how the Garbage Collector would handle cleanup of objects created on either side, if they are referenced / destroyed on the ‘other side’.
So far, I’ve read various articles and forum questions on StackOverflow and MSDN, which has lead me to believe that the Garbage Collector should work across both types of code when running in the same process – i.e if an object was created in C# and passed to the C++/CLI bridge, it would not be collected until the references on both sides were no longer in use.
My question in this case, breaks down into three parts:
- Am I right in concluding that the Garbage Collector works across both portions of code (C# and C++/CLI) when running in the same process?
- In relation to 1: how does it work in such a circumstance (specifically in terms of cleaning up objects referenced by both code bases).
- Are there any suggestions on how to monitor the activity of the Garbage Collector – i.e. writing tests to check when Garbage Collection occurs; or a program that monitors the Garbage Collector itself.
I already have somewhat of an understanding of how the Garbage Collector works in general, so my questions here are specific to the following scenario:
Components
- Assembly A – (written in C#)
- Assembly B – (written in C++/CLI)
Program Execution
- Object
Ois created in Assembly A. - Object
Ois passed into a function inside Assembly B. - Reference to object
Oin Assembly A is released. - Assembly B holds onto reference to object
O. - Execution ends (i.e. via program exit).
- Assembly B releases reference to object
O.
Thanks in advance for any thoughts on this question. Let me know if further information is needed or if something is not clear enough.
EDIT
As per request, I have written a rough example of the scenario I’m trying to describe. The C# and C++/CLI code can be found on PasteBin.
When the code is actually running, none of it will be C# or C++/CLI. All of it will be IL from the C# and C++/CLI and machine code from the native code you’re interoperating with.
Hence you could re-write part of your question as:
Of the managed objects, all of them will be garbage collected as per the same rules, unless you use a mechanism to prevent it (GC.KeepAlive). All of them could be moved in memory unless you pin them (because you’re passing addresses to unmanaged code.
.NET Profiler will give you some information on garbage collection, as will the collection counts in performance monitor.