Garbage Collected Classes

Classes under C++ are usually allocated on the stack or the heap as required. Classes allocated with the new keyword must also be deleted in an orderly fashion to prevent memory leaks. Garbage Collected classes are allocated on the GC heap, are subject to lifetime management and movement by the GC, and will be deleted from the system only after the final reference to them has been released.

The declaration for a Garbage Collected class under managed C++ is as follows:

__gc class SomeClass
{
};

Under .NET, all classes that have no explicit base class are implicitly derived from System::Object. The managed C++ classes declared with __gc are also added to in this manner by the compiler. This base class has methods that ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.