【发布时间】: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 我找到了解决方案,请参阅我的答案