【问题标题】:OpenCV allocation causes segfault in std::thread::joinOpenCV 分配导致 std::thread::join 中的段错误
【发布时间】:2016-01-10 03:15:36
【问题描述】:

下面的代码在 std::thread 类的 .join() 中引发了分段错误。但是,只有我使用 cv::fastMalloc 分配数据数组时才会发生这种情况。如果我使用 'new' 关键字或 std::malloc 函数,则不会发生错误。

我需要了解为什么会发生此错误,因为实际上我需要一个使用此函数的 cv::Mat。

int main() {
    uchar* data = (uchar*) cv::fastMalloc(640);

    std::atomic<bool> running(true);
    std::thread thread([&] () {
        while(running) {
            // I'll perform some process with data here
            // for now, just to illustrate, I put thread to sleep
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    });
    std::this_thread::sleep_for(std::chrono::seconds(1));

    running = false;
    // segfault is thrown here
    thread.join();

    cv::fastFree(data);
    return 0;
}

GDB 调用栈如下所示

#0 00429B26 _pthread_cleanup_dest () (??:??)
#1 003E32A0 ?? () (??:??)

有人知道会发生什么吗?我真的觉得这太疯狂了:S。

谢谢。

【问题讨论】:

  • 您使用的是哪个版本?你是如何编译/链接的?如果这一切都很好,看起来像一个明确的错误。
  • mingw32-g++.exe -std=c++11 -Wall -fexceptions -lm main.cpp -o main.o mingw32-g++.exe -o main.o -lopencv_core2410.dll.a MinGW 4.8.1 和 OpenCV 2.4.10
  • 不知道 mingw,但通常当您使用 std::thread 时,您还需要链接 -lpthread
  • 是的,我已经试过了,没有效果。我也尝试设置-pthreadcompiler 标志,但也没有用。

标签: c++ multithreading opencv c++11


【解决方案1】:

我通过重新安装 opencv 解决了这个问题。显然问题是我编译了 opencv 并在这个例子中使用的不同版本的编译器。

为了记录,我前段时间用不支持 std::thread 的 MinGW 版本编译了 opencv(我认为是 4.7.x)。

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多