句柄机制思考


实体拓扑对象处理中的一个难点,是共享对象的管理,这一点非常重要,对3D几何造型引擎非常关键。如果处理不好,轻则内存泄漏,重则系统不稳定,会因为共享对象产生的内存错误,可能导致莫名的死机问题。

而在实体拓扑结构中,共享对象管理是个本质的问题,是没法回避的。即使不考虑其他拓扑对象的共享,有向边共享共边对象这一事实(ParasolidFinEdgeACISCoEdgeEdge),就导致必须有相应的共享对象管理机制。另外,如果缺乏对象管理机制,一些算法的实现上,会很困难。

OCCT的整体实现中的共享对象管理,基于句柄机制,主要是基于如下类(OCCT720):

Template class handle; (Standad_Handle.hxx)

该类基于引用计数机制,实现了共享对象指针的智能管理技术。

然而,由于引用计数机制有一个缺点:没法解决循环引用问题,WikiPedia中,谈到了引用计数两大disavantages之一是:

The naive algorithm described above can't handle reference cycles, an object which refers directly or indirectly to itself. A mechanism relying purely on reference counts will never consider cyclic chains of objects for deletion, since their reference count is guaranteed to stay nonzero. Methods for dealing with this issue exist but can also increase the overhead and complexity of reference counting — on the other hand, these methods need only be applied to data that might form cycles, often a small subset of all data. One such method is the use of weak references, while another involves using a mark sweep algorithm that gets called infrequently to clean up.

因此,OCCT的拓扑结构实现,采用了单向包含关系,即每个拓扑对象只记录自己的子对象,而子对象不记录对父对象的引用。

这一点,就决定了OCCT的拓扑结构,要获取反向关系,例如:知道Edge属于那个Face,就需要通过加表机制。这一点,会很影响算法的实现效率。