Visual C++ GC Interface How to enable it and which library to include

Viewed 173

I started learning GC interface in C++ and ran a few examples available on web in Visual 2019 C++ for below functions:

Each reference above has an example code which got compiled in my VC++ 2019 and successfully executed

What I am missing to understand that does VS2019 has GC by default enabled - I mean don't we need to enable some compiler switches or library to add for linking.

Also if GC is defautly enabled then why we do not have 'set_pointer_safety' but 'get_pointer_safety' only?

Then how can we use the GC features in VC++ some example that the memory is de-allocated as a result of GC?

2 Answers

I started learning GC interface in C++

Did you read the C++11 standard n3337 ?

Visual C++ GC Interface How to enable it and which library to include

As others explained, there is no real GC in Visual C++

Writing a simple garbage collector is fairly easy.

Just read the GC handbook for guidance, and/or the Dragon Book, and/or Lisp In Small Pieces or this Uniprocessor Garbage Collection Techniques paper.

Notice that C++ garbage collection marking or copying routines for your GC could be generated by tools similar to SWIG in the spirit of the s11n library.

If your code base is small (a few dozen thousands of C++ lines, and a few dozens of C++ class-es) you might even generate GC support routines with your own metaprogram (or using the GPP preprocessor or the GNU m4 one or GNU gawk). If you can and are allowed to compile your C++ code with a recent GCC (e.g. GCC 10 in July 2020), consider writing your GCC plugin to generate garbage collection support C++ code. See also [this draft][9] report, and the references inside it. Notice the garbage collector inside GCC.

You could be interested by Boehm's conservative GC library, or by Frama-C, or by the Clang static analyzer.

But writing a state of the art and high-performance GC takes a lot of efforts, in particular when your C++ program is multi-threaded.

PS. On both github and gitlab and elsewhere you'll find several open source implementations in C or C++ of garbage collected languages, e.g. this (or GNU guile or Python or GHC or Ocaml). I recommend studying -for inspiration- their C++ or C source code. And GCC has internally its own garbage collector (see also my old unmaintained GCC MELT project and several slides I wrote about it).

Visual C++ does not implement garbage collection, so the questions of whether/how to enable it or which libraries it requires are moot.

The presence of the listed functions does not mean that a GC exists. It only means that VC++ implements the interfaces mandated by C++11 that would allow a GC to work. But there is none such provided as of the latest version 2019, and the VC++ implementation of those functions is just no-ops, with the "pointer safety model" returned as pointer_safety::relaxed i.e. none at all. Quoting from the VC++ memory header:

// GARBAGE COLLECTION
enum class pointer_safety { relaxed, preferred, strict };

inline void declare_reachable(void*) {}

template <class _Ty>
_Ty* undeclare_reachable(_Ty* _Ptr) {
    return _Ptr;
}

inline void declare_no_pointers(char*, size_t) {}

inline void undeclare_no_pointers(char*, size_t) {}

inline pointer_safety get_pointer_safety() noexcept {
    return pointer_safety::relaxed;
}

From Stroustrup's GC ABI FAQ:

relaxed: safely-derived and not safely-derived pointers are treated equivalently; like C and C++98 [...]

More on SO about C++11 GC:

Related