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

295 天前
 StarlightCX

请教下各位大佬,这是个什么问题?我在解码视频帧的时候想用 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();
    }
};

912 次点击
所在节点    C++
2 条回复
ysc3839
295 天前
调试,看怎么 crash 的
GreyWang
140 天前
有可能是这个异常太底层,并没有被捕获到;
建议尝试下这个更基础的 try-except 方法,将异常的处理方法放到 except 中处理即可

__try
{}
__except(1)
{}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/957945

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX