简体   繁体   English

访问包含boost :: shared_ptr的std :: vector <CustomObj> -&gt; at()是否返回共享指针的副本?

[英]Accessing std::vector containing boost::shared_ptr<CustomObj> -> does at() return a copy of shared pointer?

i am new to boost::shared_ptr using it the first time now. 我现在是第一次使用boost :: shared_ptr。 I have a std::vector containing boost::shared_ptr which i "filled" with objects created from a custom class. 我有一个std :: vector包含boost :: shared_ptr,我用从自定义类创建的对象“填充”了它。

In Code: std::vector<boost::shared_ptr<Foo>> data; 在代码中: std::vector<boost::shared_ptr<Foo>> data;

The vector is created inside a function on the stack. 向量是在堆栈上的函数内部创建的。 I want to access some elements inside my vector using vector.at(k). 我想使用vector.at(k)访问向量中的某些元素。 The retured shared_ptr will be send to another thread, lets call him T1. 撤销的shared_ptr将发送到另一个线程,让他称为T1。 My vector runs out of scope while T1 is still processing the shared_ptr. T1仍在处理shared_ptr时,我的向量超出了范围。

In Code: 在代码中:

void myFunction(){

  //lets get my object boost::shared_ptr<Foo> obj = data.at(k); //emit it to Thread T1 (i am using QT) emit sendObjToThread1(obj); 

}

I previously thought this would give no problem, but since my program is acting very strange, i am about to changing my mind. 我以前认为这不会有任何问题,但是由于我的程序表现得很奇怪,所以我将改变主意。 :) To clarify this i am asking you on this platform now. :)为了澄清这一点,我现在在这个平台上问你。 :) :)

Is it right, that my object will be destroyed if my vector runs out of scope? 是的,如果我的向量超出范围,我的对象将被破坏吗? If yes, how can i manage to get a deep copy of the shared_ptr (but not the object it holds). 如果是,我如何设法获取shared_ptr的深层副本(而不是它所保存的对象)。 Just to have mentioned it: My application is based on QT. 刚刚提到:我的应用程序基于QT。

Thx for reading. 谢谢你的阅读。

See one of the docs for std::vector<T>::at() . 请参阅文档之一以了解std::vector<T>::at() It returns a reference. 它返回一个参考。 As long as your emit sendObjToThread1(obj); 只要您emit sendObjToThread1(obj); does a copy of the shared_ptr you are safe. 确保您安全的shared_ptr副本。

No. It returns a reference. 否。它返回参考。

boost::shared_ptr<Foo> obj = data.at(k);

But in this statement you are making a copy by creating obj. 但是在此语句中,您是通过创建obj进行复制的。

boost::shared_ptr<Foo>& obj = data.at(k);
                     ^^^

Now there are no copies made. 现在没有副本。

Edit: 编辑:

What about this line? 那条线呢?

sendObjToThread1(obj);

Are you passing be reference or value? 您传递的是参考还是价值? If you pass by reference to another thread then this is a problem as the local copy of obj is about to die (as soon as the function returns). 如果您通过引用传递给另一个线程,那么这将是一个问题,因为obj的本地副本即将死亡(该函数返回时)。 So you must pass obj by value to make sure the other thread has its own copy and increments the shared_pointer counter appropriately. 因此,您必须按值传递obj以确保另一个线程具有其自己的副本,并适当地增加shared_pointer计数器。

I think you're using threads. 我认为您正在使用线程。 Go read this and double-check everything related to thread safety. 阅读并仔细检查与线程安全有关的所有内容。 Make sure you're using either the pthreads or lock-free implementation. 确保您正在使用pthreads或无锁实现。

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

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