如果(mySharedPtr)导致信号11

if(mySharedPtr) causes Signal 11?

本文关键字:信号 mySharedPtr 如果      更新时间:2023-10-16

不知何故,在不取消引用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)