Memory corruption on cv::cuda::warpPerspective

asked 2019-03-29 05:17:59 -0500

Main Problem: Memory Corruption.

Guess on the cause: I think it's a concurrency problem on GPU usage.

Info of the system:

  • Ubuntu 16.04
  • Opencv 3.4.4
  • CUDA 9.2

Code: (actually the main part I copy from somewhere else, but the memory corruption issue still occured)

#include "opencv2/opencv.hpp"
#include <iostream>
int main( ){
    cv::Mat src_img, dst_img;
    cv::cuda::GpuMat src_gpu, dst_gpu;
    src_img = cv::imread("../../test_img.png",CV_LOAD_IMAGE_GRAYSCALE);
    if (src_img.empty()) {
            std::cout<<"error: can not load image...\n"<<std::endl;
            return -1;
    }
    src_gpu.upload(src_img);
    std::cout<<"src img size:"<<src_img.size()<<std::endl;
    dst_img.create(src_img.size(), src_img.type());
    std::cout<<"src img type:"<<src_img.type()<<std::endl;
    std::cout<<"dst img type:"<<dst_img.type()<<std::endl;
    dst_gpu.upload(dst_img);

    cv::Point2f src_p[4];
    cv::Point2f dst_p[4];

    float w  = (float)src_img.cols;
    float h  = (float)src_img.rows;
    float hw = w / 2.0f;
    float hh = h / 2.0f;

    src_p[0] = cv::Point2f(0.0f, 0.0f);
    src_p[1] = cv::Point2f(   w, 0.0f);
    src_p[2] = cv::Point2f(   w,    h);
    src_p[3] = cv::Point2f(0.0f,    h);

    dst_p[0] = cv::Point2f(  hw, 0.0f);
    dst_p[1] = cv::Point2f(   w,   hh);
    dst_p[2] = cv::Point2f(  hw,    h);
    dst_p[3] = cv::Point2f(0.0f,   hh);

        cv::Mat trans_mat33 = cv::getPerspectiveTransform(src_p, dst_p); //CV_64F->double

            std::cout<<"start warpPerspective"<<std::endl;
            cv::cuda::warpPerspective(src_gpu, dst_gpu, trans_mat33, src_img.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT,cv::Scalar(),cv::cuda::Stream::Null());
            std::cout<<"finish warpPerspective"<<std::endl;

            std::cout<<"start start download src img"<<std::endl;
            src_gpu.download(src_img,cv::cuda::Stream::Null());
            std::cout<<"start start download dst img"<<std::endl;
            dst_gpu.download(dst_img,cv::cuda::Stream::Null());
            std::cout<<"finish download img"<<std::endl;

     //       std::cout<<"dst img at (3,3):"<<dst_img.at<uchar>(3,3)<<std::endl;
            std::cout<<"start write img"<<std::endl;
            cv::imwrite("warp_result.jpg", dst_img);
            std::cout<<"program finish"<<std::endl;


            return 0;
     }

Output Case 1:

src img size:[2880 x 1800]
src img type:0 
dst img type:0
start warpPerspective
*** Error in `./online_test': malloc(): memory corruption: 0x00000000031c3ea0 ***

Output Case 2:

 src img size:[2880 x 1800]
 src img type:0
 dst img type:0
 start warpPerspective
 finish warpPerspective
 start start download src img
 start start download dst img
 finish download img
 start write img
 *** Error in `./online_test': free(): invalid next size (fast): 0x00000000039ce1a0 ***

Ideas:

  • As here said, the cuda function should be synchronous by default, so I'm curious about why would I get different result from the same code?
  • I occasionally run the code successfully, but it just by luck. Therefore, I think the problem should be mainly due to concurrency.
edit retag flag offensive close merge delete