简体   繁体   English

嵌套的boost :: shared_ptr use_count没有更新

[英]nested boost::shared_ptr use_count not updating

I have a nested boost::shared_ptr which was occasionally getting destroyed when getting assigned to another one and going out of scope. 我有一个嵌套的boost :: shared_ptr,当被分配到另一个并且超出范围时偶尔会被破坏。 I figured out that the use_count was not updating unless I copy the pointer to a temp. 我发现use_count没有更新,除非我将指针复制到temp。 The code is self-explanatory. 代码不言自明。 In the first for-loop, the use_count doesn't update, whereas it updates on the other. 在第一个for循环中,use_count不会更新,而另一个则更新。

#include <vector>
#include <boost/shared_ptr.hpp>
#include <iostream>
using namespace std;



int main(int argc, char const *argv[])
{
  typedef int T;
  typedef std::vector<T> content_1d_t;
  typedef boost::shared_ptr<content_1d_t> storage_1d_t;
  typedef std::vector<storage_1d_t> content_2d_t;
  typedef boost::shared_ptr<content_2d_t> storage_2d_t;

  int dim1 = 10;
  int dim2 = 1;
  content_2d_t* content_1 = new content_2d_t();
  content_1->reserve(dim2);
  storage_2d_t storage_1(content_1);

  for (int i = 0; i < dim2; ++i)
  {
    storage_1->push_back(storage_1d_t(new content_1d_t(dim1)));
  }

  //content_2d_t* content_2 = new content_2d_t(dim2);
  storage_2d_t storage_2 = storage_1;

  for (int i = 0; i < dim2; ++i)
  {
    cout<< "use count before : "<< storage_1->operator[](i).use_count()<<endl;
    storage_2->operator[](i) = storage_1->operator[](i);
    cout<< "use count after: "<< storage_1->operator[](i).use_count()<<endl;
  }

  for (int i = 0; i < dim2; ++i)
  {
    cout<< "use count before : "<< storage_1->operator[](i).use_count()<<endl;
    storage_1d_t ref = storage_1->operator[](i);
    storage_2->operator[](i) = ref;
    cout<< "use count after: "<< storage_1->operator[](i).use_count()<<endl;
  }


  /* code */
  return 0;
}

output 产量

use count before : 1 使用前的计数:1

use count after: 1 使用后计数:1

use count before : 1 使用前的计数:1

use count after: 2 使用后计数:2

Since you do storage_2d_t storage_2 = storage_1; 既然你做storage_2d_t storage_2 = storage_1; obviously, assigning the elements directly back onto themselves should not increase use counts. 显然,将元素直接分配回自身不应该增加使用次数。

I the second loop you print the use count during the time where you hold the temporary copy ( ref ). 我是第二个循环,您在持有临时副本( ref )期间打印使用计数。 Making it explicit, you can see that indeed - as expected - the "after" count is not actually higher: 说明一点,你可以看到确实 - 正如预期的那样 - “后”计数实际上并不高:

for (int i = 0; i < dim2; ++i) {
    cout << "use count before : " << (*storage_1)[i].use_count() << endl;
    {
        storage_1d_t ref = (*storage_1)[i];
        (*storage_2)[i] = ref;
        cout << "use count during: " << (*storage_1)[i].use_count() << endl;
    }
    cout << "use count after: " << (*storage_1)[i].use_count() << endl;
}

Now prints 现在打印

use count before : 1
use count during: 2
use count after: 1

See it Live On Coliru 看到Live On Coliru


Brainwave Did you mean to deep-clone storage_1 into storage_2 ? Brainwave你的意思是将storage_1深度克隆到storage_2吗? It appears you have been confused by the fact that your outer storage_2d_t is also a shared pointer and hence you refer to the same vector by reference. 您的外部storage_2d_t 也是共享指针这一事实似乎让您感到困惑,因此您通过引用引用相同的向量。

storage_2d_t storage_2 = boost::make_shared<content_2d_t>(*storage_1);
// or
storage_2d_t storage_2 = boost::make_shared<content_2d_t>(storage_1->begin(), storage_1->end());

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

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