【问题标题】:gnuradio c++ connect self() throw bad_weak_ptrgnuradio c++ connect self() throw bad_weak_ptr
【发布时间】:2019-02-24 13:28:30
【问题描述】:

我想在我的构造函数中调用一些代码

connect(self() , 0 , filter , 0);
connect(filter , 0 , self() , 0);

但我得到异常

在抛出 'boost::exception_detail::clone_impl >' 的实例后调用终止

下一个

my_filter::sptr
my_filter::make(unsigned int interpolation,
            unsigned int decimation) {
auto ptr = gnuradio::get_initial_sptr(new my_filter
                     (interpolation, decimation));
ptr->wire();

return ptr;

}

和方法线

void my_filter::wire() {
connect(self(),    0, resampler,  0);
connect(resampler, 0, self(),     0);
 }

但我得到错误

Terminate called after throwing an instance of 'std::invalid_argument'
what():  sptr_magic: invalid pointer!
 what():  tr1::bad_weak_ptr

我该如何改进?

【问题讨论】:

  • 我们需要一个minimal reproducible example,但大概在您调用self() 时,self() 包含的任何对象当前都不属于std::shared_ptr

标签: c++ gnuradio


【解决方案1】:

Read when this exception is thrown:

std::bad_weak_ptr 是作为异常抛出的对象的类型 以 std::weak_ptr 作为 std::shared_ptr 的构造函数 参数,当 std::weak_ptr 引用已删除的对象时。

很可能您的self() 只是调用shared_from_this() 并且没有指向当前对象的shared_ptr,因为您正处于构建时间,所以shared_from_this() 必须抛出异常。

两个修复它使用模式两步初始化。

std::tr1::shared_ptr<YourCalsss> YourCalsss::Create() // static method
{
    auto result = std::tr1::shared_ptr<YourCalsss>(new YourCalsss);
    result->init(); // inside that you will do a connect
    return result;
}

PS。我假设您使用 C++03 和 tr1,因为错误信息提供了这个线索。

【讨论】:

  • 在 gnuradio 中使用 make() 方法
  • 类块:虚拟公共 gr::hier_block2 {
  • public: typedef boost::shared_ptr sptr;静态 sptr make(); };
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多