Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcroot in c++/cli

Tags:

c++-cli

What does gcroot mean? I found it in code I am reading.

like image 983
lital maatuk Avatar asked Feb 15 '11 15:02

lital maatuk


People also ask

What is Gcroot in C++?

gcroot is a C++/cli template class that eases holding managed types in C++/cli classes.

What is vcclr h?

vcclr. h provides the type-safe wrapper template gcroot to refer to a CLR object from the C++ heap. This template lets you embed a virtual handle in a native type and treat it as if it were the underlying type. In most cases, you can use the gcroot object as the embedded type without any casting.


1 Answers

When the .NET garbage collector runs, it determines which objects are still in use by doing reachability analysis. Only the managed heap is analyzed while looking for pointers to objects, so if you have a pointer from a native object to a managed object, you need to let the garbage collector know, so it can include it in reachability analysis, and so it can update the pointer if the target moves during compaction.

As rstevens said, the .NET GCHandle class does this, and C++/CLI is a C++-oriented wrapper for GCHandle which adds type safety and convenient syntax.

like image 191
Ben Voigt Avatar answered Sep 28 '22 10:09

Ben Voigt