简体   繁体   English

带有观察者指针的共享指针

[英]Shared pointer with observer pointers

I have a class Foo, which has a vector of some large classes. 我有一个类Foo,它有一些大类的向量。 The idea is, that an octal tree will be built recursively out of the elements of the vector, and each OctreeNode will have a pointer to few elements of the vector found in Foo. 这个想法是,八进制树将以向量的元素递归地构建,并且每个OctreeNode将具有指向Foo中找到的向量的少数元素的指针。 (In the example, just for simplicity, a node will point to only one element of the vector) (在示例中,为简单起见,节点将仅指向向量的一个元素)

class Foo
{
    vector<LargeClass>  mLargeClasses;

    void removeItem(const int index);    //remove an element from the vector at the index
}

class OctreeNode
{
    LargeClass* mLargeClass;
}

One can say, "why bother keeping the vector after the tree is built, and store the objects in the tree itself". 可以说,“为什么在构建树之后保留向量,并将对象存储在树本身中”。 True, let's just say, I need to keep vector parallel to the built tree as well. 是的,我只想说,我需要保持矢量与构建的树平行。

While the above concept works, I have issues when elements got removed from the underlying vector. 虽然上述概念有效,但是当从底层向量中删除元素时,我遇到了问题。 In such case, some Octree nodes end up with dangling pointers. 在这种情况下,一些八叉树节点最终会出现悬空指针。

My solution #1: If removeItem function is called, then before it removes the vector element, it first recursively traverse the octal tree, and make all mLargeClass pointer a nullptr which happen to point to that particular vector element. 我的解决方案#1:如果调用removeItem函数,那么在它移除向量元素之前,它首先递归遍历八进制树,并使所有mLargeClass指针成为偶然指向该特定向量元素的nullptr。 It's ok to have nullptr in the nodes, as I check each time against nullptr, when I access them anyway. 可以在节点中使用nullptr,因为我每次都会检查nullptr,无论如何我都会访问它们。

My solution #2: Have the vector store shared_ptrs , and have the OctreeNode store a weak_ptr . 我的解决方案#2:让vector存储shared_ptrs ,并让OctreeNode存储一个weak_ptr I am not fan of this, as each time I access a weak_ptr in the tree, it gets converted to a shared_ptr in the background with all the atomic counter increases. 我不喜欢这个,因为每次我访问树中的weak_ptr时,它会在后台转换为shared_ptr ,并且所有原子计数器都会增加。 I am not expert on performance testing, but I have a feeling, that it is slower than a simple pointer access with if condition. 我不是性能测试的专家,但我有一种感觉,它比使用if条件的简单指针访问慢。

Does anybody know any better solutions? 有人知道更好的解决方案吗?

I think the most elegant would be: To have smart pointer which behaves like a shared_pointer , counts, how many other pointer refers to it, keep a record of them, and in case it gets destroyed, it automatically nulls out all other "observer" pointers which refer to it? 我认为最优雅的是:要有智能指针,其行为类似于shared_pointer ,计数,有多少其他指针引用它,保留它们的记录,如果它被破坏,它会自动将所有其他“观察者”归零指向它的指针?

While the field, and the purpose is somewhat different, i think i will give a try for the handle system described in this port: Simple, efficient weak pointer that is set to NULL when target memory is deallocated If I fail, I will revert back to the shared_ptr/weak_ptr duo. 虽然这个领域和目的有些不同,但我想我会尝试一下这个端口中描述的句柄系统: 简单,高效的弱指针,当目标内存被释放时设置为NULL如果我失败了,我会回复到shared_ptr / weak_ptr二重奏。 Described in the same post. 在同一篇文章中描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM