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) {
  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);
    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.