[英]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
我该如何改善?
抛出此异常时读取 :
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;
}
PS。 我假设您使用的是带有tr1
C ++ 03,因为错误信息提供了这一线索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.