VBA Garbage Collector Details

后端 未结 1 606
长情又很酷
长情又很酷 2020-12-16 14:06

I\'ve found myself having to write some VBA code recently and just wondered if anyone had ever come across any details on how the VBA garbage collector works? The .Net GC is

相关标签:
1条回答
  • 2020-12-16 14:22

    The following assumes that VBA is still using the same garbage collection mechanism used in VB6 (which it very probably does).

    VB6 used a reference-counting GC. The GC is triggered deterministically when the last reference to a given object is set to Nothing. Setting local references to Nothing is unnecessary, this happens as they go out of scope.

    Every object implements a COM interface that takes care of the reference count for that object. Each assignment of an object reference updates the reference counters of the involved references (i.e. the counter of old object that was previously referenced gets decremented, and the new object’s counter is incremented). An object is garbage collected when its reference counter reaches 0.

    Objects in circular references are thus never collected during the lifetime of a VBA application. What’s more, VBA doesn’t offer a way to break circular references. In VB6, weak references could be implemented via WinAPI functions.

    0 讨论(0)
提交回复
热议问题