Skip to content

UMat refcount Error when setting UseOpenCL to false #8693

Closed

Description

@theaniolad
System information (version)
  • OpenCV => 3.2
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2015
Detailed description

I'm currently changing all Mats in my program to UMats and have a crash with some functions (cv::convertTo and cv::reduce) which I don't think should happen.

  1. Code runs fine with Mats
  2. Code runs fine with UMats if UseOpenCL set to true
  3. Code crashes with UMats if UseOpenCL set to false
Steps to reproduce
ocl::setUseOpenCL(false);
auto src = UMat::zeros({ 10,10 }, CV_32FC1);
reduce(src, src, 0, CV_REDUCE_SUM);  //crashes with refcount Error
src.convertTo(src, CV_64FC1); //crashes with refcount Error

The error is:
OpenCV Error: Assertion failed (u->refcount == 0) in cv::StdMatAllocator::deallocate, file opencv\sources\modules\core\src\matrix.cpp, line 214

Now you might say that I should simply use a different output UMat, but I would also like to write functions like this:

void foo(cv::InputArray src, cv::OutputArray dst)
{
   cv::reduce(src, dst, 0, CV_REDUCE_SUM);
   //do other stuff
}
   
int main()
{
   ocl::setUseOpenCL(false);
   auto src = UMat::zeros({ 10,10 }, CV_32FC1);
   foo(src, src);
}

Currently I'm using the following fix to make the program run for all three cases:

 void foo(cv::InputArray src, cv::OutputArray dst)
{
     if (src.isUMat() && !ocl::useOpenCL())
    {
        cv::reduce(src.getUMat(), dst, 0, CV_REDUCE_SUM);
    }
     else 
    {
        cv::reduce(src, dst, 0, CV_REDUCE_SUM);
    }
}

Activity

  1. changed the title [-]UMat refcount Error when setting UseOpenCL to flase[/-] [+]UMat refcount Error when setting UseOpenCL to false[/+] on May 5, 2017
  2. dkurt commented on Sep 5, 2017

    @dkurt
    Member

    Minimal example to reproduce it (summarized from cv::reduce code):

    void func(InputArray _src, OutputArray _dst) {
      Mat src = _src.getMat();
      _dst.create(10, 1, src.type());
    }
    
    int main(int argc, char** argv) {
      ocl::setUseOpenCL(false);
      UMat src(10, 10, CV_32FC1);
      func(src, src);
    }
  3. added 2 commits that reference this issue on Sep 5, 2017
  4. alalek commented on Sep 6, 2017

    @alalek
    Member

    Related: #8352

  5. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCcategory: t-api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions