简体   繁体   English

OpenCV分配在std :: thread :: join中导致segfault

[英]OpenCV allocation causes segfault in std::thread::join

The code below throws a segmentation fault inside the .join() of the std::thread class. 下面的代码在std :: thread类的.join()内部引发了分段错误。 However, that is happen only I use cv::fastMalloc to allocate a data array. 但是,只有在我使用cv :: fastMalloc分配数据数组时才发生这种情况。 If I use the 'new' keyword or the std::malloc function no error happens. 如果我使用'new'关键字或std :: malloc函数,则不会发生错误。

I need understand why this error happens because in fact I need a cv::Mat that uses this function. 我需要了解为什么会发生此错误,因为实际上我需要使用此功能的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;
}

The GDB callstack follows below GDB调用栈如下

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

Does anyone know what might be happening? 有人知道会发生什么吗? I really think it is too crazy :S. 我真的认为这太疯狂了:S。

Thanks. 谢谢。

I solved this issue reinstalling the opencv. 我解决了重新安装opencv的问题。 Apparently the problem was the different versions of compilers that I had compiled the opencv and I'm using in this example. 显然问题是我编译了opencv并在此示例中使用的不同版本的编译器。

For the record, I had compiled the opencv some time ago with a MinGW version that not support std::thread (I think 4.7.x). 作为记录,我前段时间使用不支持std :: thread的MinGW版本编译了opencv(我认为是4.7.x)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM