global std::vector<cv::Mat> segfault during program exit

asked 2020-04-23 22:01:27 -0600

stiga gravatar image

The following code crashes during program exits. GDB shows that during vector destruction it triggers segfault.

#include <experimental/filesystem>
#include "opencv2/opencv.hpp"
namespace fs = std::experimental::filesystem;
std::vector<cv::Mat> imgs;
int main(int argc, char** argv) {
  cv::Mat::setDefaultAllocator(cv::cuda::HostMem::getAllocator(
      cv::cuda::HostMem::AllocType::PAGE_LOCKED));
  const char* img_dir = argv[1];
  for (const auto& entry : fs::directory_iterator(img_dir)) {
    std::string filepath = entry.path();
    cv::FileStorage fs2(filepath.c_str(), cv::FileStorage::READ);
    imgs.emplace_back();
    fs2["img"] >> imgs.back();
  }
  return 0;
}

If I make the imgs variable a local variable in the main function it does not segfault. And if I do not set the PAGE_LOCKED memory allocator it does not segfault. Could anyone explain what is the root cause of the segfault? I am using OpenCV 3.3.1.

edit retag flag offensive close merge delete