gnuradio c++ connect self() throw bad_weak_ptr

gnuradio c++ connect self() throw bad_weak_ptr

本文关键字:bad weak ptr throw c++ connect self gnuradio      更新时间:2023-10-16

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

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

我该如何改进?

当引发此异常时读取:

std::bad_weak_ptr 是作为异常抛出的对象的类型 将 std::shared_ptr 的构造函数 std::weak_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;
}

附言。我假设您正在使用 C++03 和tr1因为错误信息提供了此线索。