c++ - 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::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
因为错误信息提供了这个线索。
推荐阅读
- javascript - 同一个 SPA 中的后退按钮
- python - 实现的排序算法性能不佳
- mysql - 通过重置 id 的 table2 导出与 table3 的关系的 table1;
- android - 已发布的 Flutter 应用程序在启动时崩溃
- python - 有向图并行排序
- jquery - 如何使用ajax打开精美的盒子图像拇指?
- c# - Aforge Unity3D 集成 IPCam 图像卡顿
- python - Image is not displaying at runtime in panel, wxpython
- angular - ionic Angular ngModel:内部数组的 ngFor 循环
- laravel - 如何为更新表单 Laravel 制作动态下拉列表