Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Random segfaults when checking for OpenCL

Hi, I wanted to use UMats to speed up some OpenCV calls to speed up performance, but ran into an issue when checking for OpenCL. Basically I broke everything down to this to reproduce the error:

#include <stdlib.h>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <opencv2/core/ocl.hpp>

using namespace cv;

struct dataStruct{
        Mat     m1;
        Mat     m2;
        Mat     m3;
        Mat     m4;
        Mat     m5;
        Mat     m6;

Mat createMat(){
        Mat m;
        m = imread("test.png", CV_LOAD_IMAGE_COLOR);
        return m;

int main(int argc, char** argv){

        if  ( ! cv::ocl::haveOpenCL ())
                printf("no openCL");

        struct dataStruct* data = (struct dataStruct*)malloc(sizeof(struct dataStruct));

        data->m1.create(1920, 1080, CV_8UC4);
        data->m2.create(1920, 1080, CV_8UC4);
        data->m3.create(1920, 1080, CV_8UC4);
        data->m4.create(1920, 1080, CV_8UC4);
        data->m5.create(1920, 1080, CV_8UC4);
        data->m6.create(1920, 1080, CV_8UC4);

        Mat x1 = createMat();
        Mat x2 = createMat();
        Mat x3 = createMat();
        Mat x4 = createMat();
        Mat x5 = createMat();
        Mat x6 = createMat();

        data->m1 = x1;
        data->m2 = x2;
        data->m3 = x3;
        data->m4 = x4;
        data->m5 = x5;
        data->m6 = x6;

        return 0;


This code provokes a segfault in about half the times I run it, the other times the output is "openCL" as expected. I built OpenCV from the repo with -D WITH_OPENCL=ON, do I need to do anything else?

I ran into this error both on Mac OS X and on Android, the behaviour is the same. If I delete the if statement for OpenCL everything works fine on every try on both systems.