Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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.