首页 > 解决方案 > gnuradio c++ connect self() throw bad_weak_ptr

问题描述

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

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

我怎样才能改善这一点?

标签: c++gnuradio

解决方案


引发此异常时阅读

std::bad_weak_ptr 是当 std::weak_ptr 引用已删除对象时,以 std::weak_ptr 作为参数的 std::shared_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因为错误信息提供了这个线索。


推荐阅读