StarlightCX
V2EX  ›  C++

C++菜鸟,请教一下关于异常捕获的问题

By StarlightCX at 2023 年 7 月 19 日 · 2229 次点击

请教下各位大佬,这是个什么问题?我在解码视频帧的时候想用 try catch 做一个舍帧的操作,但是 log 显示在 throw 之后并没有 catch 到相应的异常而是直接 crash 了。

环境: MacOS 下 ndk-r25c 交叉编译

CMAKE_CXX_FLAGS = -std=c++17 -flto -fexceptions -D__STDC_CONSTANT_MACROS

代码:


// 在子线程中执行
while(true) {
	...
	try {
        auto nframe = getVideoFrame();
        frame = std::move(nframe);
    } catch(const GetDecodedFrameException& e) {
        logW("获取视频帧未成功");
        continue;
     } catch(...) {
        logE("获取视频帧发生错误");
        break;
    }
    ...
}


std::unique_ptr<AVFrameWrapper> getVideoFrame() {
    ...

    if (ps.isDropFastFrame() || (ps.isDropFastFrame() && ps.getMasterSyncType() != SyncType::AV_SYNC_VIDEO)) {
        if (frame.getPts() != AV_NOPTS_VALUE) {
            double diff = dpts - ps.getMasterClock();
            if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD &&
                diff - ps.getLastFrameFilterDelay() < 0 &&
                getState().getPktSerial() == ps.getVideoClock().getSerial() &&
                getPacketQueue().getNbPackets() > 0) {
                ps.frameFastDropCountIncrease();
                frame.frameUnRef();
                logW(fmt::format("舍帧 diff: {}, dpts: {}", diff, dpts));
                throw GetDecodedFrameException();
            }
        }
    }

    ...
}

class GetDecodedFrameException : public std::exception {
private:
    std::string info;
public:
    GetDecodedFrameException(): std::exception() {
        info = "GetDecodedFrameException";
        logE(info);
    };
    const char * what () const noexcept {
        return info.c_str();
    }
};

2 条回复  •  2023-12-21 17:35:44 +08:00
ysc3839
   1
ysc3839  
   2023 年 7 月 19 日 via Android
调试,看怎么 crash 的
GreyWang
   2
GreyWang  
   2023 年 12 月 21 日
有可能是这个异常太底层,并没有被捕获到;
建议尝试下这个更基础的 try-except 方法,将异常的处理方法放到 except 中处理即可

__try
{}
__except(1)
{}
© 2026 V2EX · 32ms · 3.9.8.5