Segmentation Fault with OpenCV 3.0.0 on Windows 8.1 with MinGW

asked 2015-10-31 15:10:32 -0500

I have a simple multi-threaded version with TBB which works fine on a linux machine but on occasions, not always, gives segmentation fault when executed on windows.

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <dirent.h>
#include <string.h>
#include <chrono>
using namespace cv;

std::vector<std::string> fnames;
class Parallel_process : public cv::ParallelLoopBody
        Parallel_process() {}

        void operator()(const cv::Range &r) const {
            cv::Mat src, dst, color_dst;
                if(!(src=imread(fnames[r.start], 0)).data)
            Canny( src, dst, 50, 200, 3 );
            cvtColor( dst, color_dst, CV_GRAY2BGR );

            std::vector<Vec4i> lines;
            HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
            for( size_t i = 0; i < lines.size(); i++ )
                    line( color_dst, Point(lines[i][0], lines[i][1]), Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
            std::string ext = "_parallel.bmp";  
            std::size_t found = fnames[r.start].find_last_of("/\\");
            std::string fname = fnames[r.start].substr(found+1);
            std::string out_fname = fname.substr(0, fname.find(".")) + ext;
            cv::imwrite(out_fname.c_str(), color_dst);

int main()
    DIR *dir;
    struct dirent *ent;
    std::string prefix = "c:\\Users\\foo\\Desktop\\sample_code\\imgs";
    if ((dir = opendir (prefix.c_str())) != NULL) {
        while ((ent = readdir (dir)) != NULL) {
            fnames.push_back(prefix + '\\' + std::string(ent->d_name));
        closedir (dir);
    auto begin = std::chrono::high_resolution_clock::now();
    cv::parallel_for_(cv::Range(0, fnames.size()), Parallel_process());
    auto end = std::chrono::high_resolution_clock::now();

Compilation is done using:

g++ -o parallel -I"c:\opencv\mingw_bld\install\include" edge_detection_parallel.cpp -L"c:\opencv\mingw_bld\install\x86\mingw\lib" -lopencv_imgproc300 -lopencv_core300 -lopencv_imgcodecs300 -lopencv_highgui300 -std=c++11

Compiler version: g++ (GCC) 4.8.1

TBB version:

#ifndef __TBB_tbb_stddef_H
#define __TBB_tbb_stddef_H

// Marketing-driven product version

// Engineering-focused interface version

// The oldest major interface version still supported
// To be used in SONAME, manifests, etc.

GDB output (two runs; one was fine and the other received the segmentation fault:

gdb parallel.exe
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
Reading symbols from C:\Users\foo\Desktop\sample_code\parallel.exe...(no
debugging symbols found)...done.
(gdb) r
Starting program: C:\Users\foo\Desktop\sample_code/parallel.exe
[New Thread 5788.0xb90]
[New Thread 5788.0x144c]
[New Thread 5788.0x168c]
[New Thread 5788.0xd1c]
[Inferior 1 (process 5788) exited normally]
(gdb) r
Starting program: C:\Users\foo\Desktop\sample_code/parallel.exe
[New Thread 620.0x1450]
[New Thread 620.0xf04]
[New Thread 620.0x100c]
[New Thread 620.0x159c]

Program received signal SIGSEGV, Segmentation fault.
[Switching ...
edit retag flag offensive close merge delete