How to use Minimal GC in VC++ 2013? [duplicate]

拟墨画扇 提交于 2021-01-27 19:52:18

问题


According to here, VC++ 2013 supports Minimal GC.

Could you guys give me some examples to illustrate its usage?

In other words, with VC++ 2013, how to use GC?

The code example I want might look like this:

auto p = gcnew int;

Are there any?


回答1:


You may be disappointed about what Minimal GC in C++11: It doesn't do garbage collection! The minimal garbage collection support in C++11 consists of two parts:

  1. There is a mandated to not "hide" pointers for everybody. When you have a pointer you are not allowed to obfuscate this pointer to the system, e.g., by writing it to a file to be read later or by using the xor-trick to create a doubly linked list while storing just one pointer. The standard speaks about safely derived pointers (the relevant clause is 3.7.4.3 [basic.stc.dynamic.safety]).
  2. The standard C++ library provides a set of interfaces which can be used to identify pointers which can't be tracked as being reachable or, once they are no longer reachable to say so. That is, you can define a set of root objects which are considered to be usable and shouldn't be considered released by any garbage collection system.

There is, however, nothing standardized which actually makes use of these facilities. Just because there is no standard, it doesn't mean that the promises as interfaces are not used, of course.

The relevant functions for the API outlined above are defined in 20.6.4 [util.dynamic.safety] and the header to include is <memory>. The functions are, briefly:

  1. void std::declare_reachable(void* p) stating that if p is non-null pointer that p is a reachable object even if a garbage collector has decided that it isn't. The function may allocate memory and, thus, throw.
  2. template <typename T> T* std::undeclare_reachable(T* p) stating that if p is a non-null pointer that p is no longer reachable. The number of calls to undeclare_reachable(p) shall not exceed the number of calls to declare_reachable(p) with the same pointer.
  3. void std::declare_no_pointers(char* p, size_t n) declares that the range of n bytes starting at p does not contain any pointers even if a garbage collectors has decided that there would be pointers insides.
  4. void std::undeclare_no_pointers(char* p, size_t n) undoes the declaration that there are no pointers in the n bytes starting at p.
  5. std::pointer_safety std::get_pointer_safety() noexcept returns if the implementation has strict pointer safety.

I think that all of these functions can basically implemented to do nothing and return a default value or an argument where a return type is specified. The pointer of these function is that there is a portable system to inform garbage collectors about pointers to consider reachable and memory areas not to trace.

In the future some level of garbage collection or, more likely, litter collection may be added but I'm not sure if there is a concrete proposal on the table. If something it is added it is probably something dubbed litter collection because it actually doesn't clean up all garbage: litter collection would just reclaim memory of unreachable object but not try to destroy the objects! That is, the system would give a view of an indefinitely living object although it may reuse the memory where it was located.



来源:https://stackoverflow.com/questions/18800897/how-to-use-minimal-gc-in-vc-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!