如果(mySharedPtr)导致信号11
if(mySharedPtr) causes Signal 11?
不知何故,在不取消引用shared_ptr
的情况下访问它会导致Android上的Signal 11(SIGSEGV)。
我在A
中有一个run()
函数,它为B
的实例获取锁并调用B::top()
。A
只有一个实例。A
有其他公共方法,其他线程可能会调用这些方法来修改mB
(从而修改互斥对象),但它们还没有被任何东西调用。
LogCat错误:
04-17 15:15:16.903:A/libc(11591):0x00000024处的致命信号11(SIGSEGV)(代码=1)
A
:类
std::thread mControllerThread;
std::mutex mBMutex;
shared_ptr<B> mB;
A() {
mB.reset( new B() );
mControllerThread = std::thread( std::bind(&A::run, this) );
}
//...
void run() {
std::unique_lock<std::mutex > lk(mBMutex);
shared_ptr<const Event> event = mB->top(B::Scope::FUTURE);
}
B
:类
shared_ptr<EventHeap> mFuture;
B() {
mFuture.reset( new EventHeap() );
}
//...
shared_ptr<const Event> top(Scope scope, int mask=EVENT_MASK_SUPPORTED) const {
shared_ptr<const Event> event;
if(scope == Scope::PAST) {
//...
} else if(scope == Scope::FUTURE) {
LOGD(LOG_TAG, "Testing mFuture ptr");
// Fails here with any of these versions
if(mFuture) {
// if(mFuture.get() != NULL) {
// if(mFuture != nullptr) {
LOGD(LOG_TAG, "Getting top from FUTURE");
event = mFuture->top(mask);
} else {
LOGE(LOG_TAG, "mFuture is null");
}
}
return event;
}
那么,在不取消引用的情况下访问智能指针怎么可能导致segfault呢?谢谢
您指向的语句测试shared_ptr是否使用非null指针初始化。
if(mFuture) // can be seen as if(mFuture.privateMember_Ptr != nullptr)
很明显,指针本身并没有被取消引用,而是访问了指针的值。因此,这个内存位置似乎无效。现在这个内存位置在哪里?它是mFuture的一部分,而mFuture本身也是B的一部分。让我们重写mFuture来展示我们真正忽略的东西:
if(this->mFuture.privateMember_Ptr != nullptr)
"this"似乎无效,您可以从顶部方法中打印它开始,并使用相同的调试"展开"堆栈。从源代码来看,"this"应该对应于类A中的mB。因此,在调用B::top()之前,可以在A::run()中打印mB。mB在A的ctor中用"new B()"初始化。您是否在某处检查此内存分配是否成功?(Android AFAIK上默认禁用异常,因此new可以返回nullptr)
相关文章:
- Qt VTK交互风格的信号到小部件
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 线程之间的布尔停止信号
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 有可能在信号处理程序中设置promise吗
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在条件变量中触发错误信号的频率是多少
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 调试和自由执行中的信号处理
- 升压信号2将插槽传递到成员功能以断开连接
- C++函数包装器来捕获某些信号
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 中止信号来自 C++ 中的中止(3) (SIGABRT)
- Qt将信号与另一个类方法连接
- C++ 信号和插槽不工作:插槽不响应事件
- Coursera :自动评分器的未知信号 11
- 如果(mySharedPtr)导致信号11