确保向量中的共享指针被正确推回 [英] Ensuring shared pointers within vector are pushed back properly

查看:76
本文介绍了确保向量中的共享指针被正确推回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一些错误的方面,这确实是我第一次特别处理共享指针.

I might have some aspects of this wrong, this is really the first time I have dealt much with shared pointers in particular.

我正在遍历一棵树.我的树由一个链表组成,带有一个共享指针向量,这些指针表示每个节点的所有子代.为了遍历,我(首先)尝试这样做:

I am working on traversing a tree. My tree consists of a linked list, with a vector of shared pointers representing all children for each node. To traverse, I am (to begin with) trying to do this:

//--------------------------------------------------------------
void setupMesh(){
    Mesh mesh;

    shared_ptr<Mesh> shared_mesh(&mesh);
    meshes.push_back(shared_mesh);

    checkChildren(root, &temp_mesh);
}

//--------------------------------------------------------------
void checkChildren(Node * temp_node, Mesh * temp_mesh){

    if(!temp_node->children.empty()){
        for(int i = 0; i < temp_node->children.size(); i++){
            if(i > 0){
                shared_ptr<Mesh> new_mesh(new Mesh);
                meshes.push_back(new_mesh);
            }

            temp_node = temp_node->children[0].get();
            checkChildren(temp_child, temp_mesh);
        }
    }  
}

我的树结构本身看起来不错,但是这与我如何遍历它以及如何跟踪指针有关.当前返回错误的访问错误.据我所知,好像我正在插入一个指向临时对象temp_node和temp_mesh的指针.

My tree structure itself seems fine, but it's more an issue with how I'm traversing it, and how I'm keeping track of pointers. It is currently returning bad access errors. From what I can tell, it looks like I am inserting a pointer to a temporary object, temp_node, and temp_mesh.

为简化我所想到的过程:

To simplify the process I had in mind:

遍历属于node [0](根)的所有子级. 对于每个孩子,对他们执行相同的循环.如果子项是child [0],则继续将其坐标添加到相同的temp_mesh对象中,但是,如果子项是另一个子项,则创建一个新的网格来存储它以及它的所有第一个子项. 任何新的网格物体都应有一个指针推回到网格物体矢量(vector>)中.

Loop through all children that belong to node[0] (root). For each of the children, perform this same loop on them. If the child is child[0], continue adding it's coordinates to the same temp_mesh object, but if it is another child, create a new mesh to store it, and all first children of it. Any new meshes should have a pointer pushed back into the meshes vector (vector>).

是否有人对我如何更有效地执行此操作有任何建议,或者在处理内存中的这些指针时我哪里出错了?

Does anyone have advice on how I could do this more efficiently, or where I'm going wrong with handling these pointers in memory.

推荐答案

当前返回错误的访问错误.

It is currently returning bad access errors.

这就是您首先要担心的问题.那是一个严重的错误.

Then that's what you should worry about as your first priority. That's a serious bug.

据我所知,好像我正在插入一个指向临时对象temp_node和temp_mesh的指针.

From what I can tell, it looks like I am inserting a pointer to a temporary object, temp_node, and temp_mesh.

这不是临时"对象,这意味着有所不同(为什么要在变量名中继续使用"temp"?),但是您对这个问题是正确的:

This aren't "temporary" objects, that means something different (why do you keep using "temp" in your variable names?), but you're right about the problem:

shared_ptr<ofMesh> shared_mesh(&temp_mesh);

这将创建一个shared_ptr,该拥有该指针&temp_mesh,并且当不再有共享该指针所有权的shared_ptr对象时,将其删除.

This creates a shared_ptr which owns the pointer &temp_mesh and so will delete it when there are no more shared_ptr objects that share ownership of that pointer.

但是该指针是一个自动变量(又称堆栈变量)的地址,该变量超出了块末尾的范围.您没有拥有"该对象,该功能的块范围会自动对其进行管理.如果您不拥有它,那么您就不能将其所有权授予shared_ptr,因为它不是您要放弃的.

But that pointer is the address of an automatic variable (aka stack variable) which goes out of scope at the end of the block. You don't "own" that object, the block scope of that function manages it automatically. If you don't own it then you can't give ownership of it to the shared_ptr, because it's not yours to give away.

当作用域结束时,自动变量temp_mesh将被自动销毁,但是仍然有shared_ptr对象持有该指针,并认为它们是它的所有者.当您尝试通过那些shared_ptr对象访问该对象时,您将在其生命周期结束后访问一个已销毁的对象.然后,当不再有拥有指针的shared_ptr对象时,它将被删除,但不是使用new创建的,因此这是一个严重的错误. (您可以在其他函数中找到正确的位置,所以我不确定为什么在setupMesh中做错了.)

When the scope ends the automatic variable temp_mesh will be destroyed automatically, but there are still shared_ptr objects that hold that pointer, thinking they own it. When you try to access the object through those shared_ptr objects you access a destroyed object, after its lifetime has ended. Then when there are no more shared_ptr objects that own the pointer it will be deleted, but it wasn't created with new so that's a serious bug. (You get this right in the other function, so I'm not sure why you've done it wrong in setupMesh).

如果希望shared_ptr拥有对象,则需要使用new创建它,或者最好使用std::make_shared创建它:*

If you want a shared_ptr to own an object you need to create it with new, or preferably create it with std::make_shared: *

shared_ptr<ofMesh> mesh = std::make_shared<ofMesh>();
mesh0->setMode(OF_PRIMITIVE_LINE_STRIP);
mesh->setupIndicesAuto();
mesh->addVertex(root->location);
mesh->addColor(ofColor(0));

meshes.push_back(shared_mesh);

checkChildren(root, mesh.get());

这将立即创建一个由shared_ptr拥有的对象,因此不存在转让shared_ptr不能拥有的东西的所有权的问题.

This creates an object that is owned by a shared_ptr right away, so there's no problem of transferring ownership of something that can't be owned by a shared_ptr.

*或您可以使用空删除器",但这对于此答案而言太高级了,因此无法使用这样的自动变量.

这篇关于确保向量中的共享指针被正确推回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆