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
O
is created in Assembly A. - Object
O
is passed into a function inside Assembly B. - Reference to object
O
in 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.