【问题标题】:Calling Callback function from different cpp causes bad_function_call从不同的 cpp 调用回调函数会导致 bad_function_call
【发布时间】:2020-02-01 18:14:11
【问题描述】:

我目前正在开发一个 OpenCV 项目,我想从不同的 cpp 调用 main.cpp 中的回调函数。
我面临的问题是,当我想调用该函数时,我得到一个 bad_function_call,因为回调函数为空。

我的 main.cpp 看起来像这样:

 PointDetection *detect;
 detect->houghCallback = [hPublisher] (std::vector<cv::Vec4i> data) {
        //do something
 };

我的 PointDetector.hpp,它包含在 PointDetector.cpp 中:

 std::function <void (std::vector<cv::Vec4i> data) houghCallback;

我的 PointDetector.cpp,我调用函数的地方如下所示:

 void PointDetector::houghDetectCb(int status, void *userData) {
        std::vector<cv::Vec4i> houghPointsResult;
        PointDetector *self = static_cast<PointDetector *>(userData);    //This includes every variable in the hpp except the houghCallback for some reason 
        self->houghCallback(houghPointsResult);                          //This causes the bad_function_call because houghCallback is Null 
 }

有人知道是否可以这样调用回调吗?

【问题讨论】:

  • 我的猜测是,self 是一个悬空指针,指向一个已经被销毁的对象。问题出在未显示的代码中。显示minimal reproducible example
  • @IgorTandetnik 我找到了解决方案,请参阅我的答案

标签: c++ opencv callback


【解决方案1】:

我找到了解决方案:我在实际 GPU 操作之前调用了 stream.enqueqeHostCallback(请参阅此问题:cuda streams: callback not getting called after stream execution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    相关资源
    最近更新 更多