title: opencv Mat deallocation memory corruption
body: <p>I am struggling with release version of my opencv wrapper function.
  The function code runs fine, but upon function block completition, memory access violation happens.
  This problem does not appear in debug mode. The segfault happens upon freeing the heap.</p>
  
  <pre><code>int Myfunc(Arr1D_floatHdl FeatArrHdl, IMAQ_Image *img, someparams 
  *Params)
  {
  ImageInfo *Info = NULL;
  //IplImage *CVImage = NULL;
  Info = (ImageInfo*)img-&gt;address;
  CheckImage(Info, Info);
  //CVImage = cvCreateImageHeader( cvSize(Info-&gt;xRes, Info-&gt;yRes), IPL_DEPTH_8U, 4);
  //CVImage-&gt;imageData = (char*)Info-&gt;imageStart;
  //CVImage-&gt;widthStep = Info-&gt;xRes*sizeof(IPL_DEPTH_8U);
  cv::Mat BGRAimg = cv::Mat(Info-&gt;yRes, Info-&gt;xRes, CV_8UC4, (char*)Info-&gt;imageStart, sizeof(CV_8UC4)*Info-&gt;xRes);
  //cv::Mat BGRAimg(CVImage);
  //cv::Mat BGRAimg = imread( "MyImg.png", cv::IMREAD_COLOR );
  cv::Mat GREYimg;
  cv::cvtColor(BGRAimg, GREYimg, CV_BGR2GRAY);
  </code></pre>
  
  <p>Here is the code where I create Mat object from user supplied data.
  I tried to create IplImage first (commented version in code) and use Mat constructor with IplImage argument, but eneded up with the same problem.
  I know I am doing something very wrong during the Mat construction, since manualy loading the image from disk does not cause the issue.</p>
  
  <p>After creating the Mat object, all its parameters are correct and the image is fine. When comparing with the grey matrix created of it, it has refcount NULL, which I have read is perfectly fine since it is supposed to keep user data intact.</p>
  
  <p>Please help.</p>
  
  <p><strong>UPDATE to give more information</strong></p>
  
  <p>Thank you for suggestions. I am obviously prone to create such errors, I am new to C/C++.
  Unfortunately, the access violation still persists.</p>
  
  <p>Here is the complete wrapper function as it is. I tried to narrow down the problem, and skipping the HOG.compute function I do no longer get memory corruption. Skipping the memcpy acrobatics in the end, I still get the memory corrupted.</p>
  
  <pre><code>int GetHOGFeatures(Arr1D_floatHdl FeatArrHdl, IMAQ_Image *img, HogParams *Params) //returns -1 on HOG window parameters missmatch
  {
  ImageInfo *Info = NULL;
  Info = (ImageInfo*)img-&gt;address;
  CheckImage(Info, Info);
  
  cv::Mat BGRAimg = cv::Mat(Info-&gt;yRes, Info-&gt;xRes, CV_8UC4, (char*)Info-&gt;imageStart, sizeof(cv::Vec4b)*Info-&gt;xRes);
  cv::Mat GREYimg;
  cv::cvtColor(BGRAimg, GREYimg, CV_BGRA2GRAY);
  
  //set params into hog object
  cv::HOGDescriptor hog;
  hog.winSize = cv::Size(Params-&gt;winsize_width, Params-&gt;winsize_height);
  hog.blockSize = cv::Size(Params-&gt;blocksize_width, Params-&gt;blocksize_height);
  hog.blockStride = cv::Size(Params-&gt;blockstride_x, Params-&gt;blockstride_y);
  hog.cellSize = cv::Size(Params-&gt;cellsize_width, Params-&gt;cellsize_height);
  hog.nbins = Params-&gt;nBins;
  hog.derivAperture = Params-&gt;derivAperture;
  hog.winSigma = Params-&gt;win_sigma;
  hog.L2HysThreshold = Params-&gt;threshold_L2hys;
  hog.gammaCorrection = (Params-&gt;gammaCorrection != 0);
  
  MgErr error = mgNoErr;
  
  cv::vector&lt;float&gt; ders;
  cv::vector&lt;cv::Point&gt; locations;
  
  try
  {
      //winstride - step of window
      //padding - borderpadding
      //raises exception with incorrect params ... todo replace trycatch with paramchecking
      hog.compute(GREYimg, ders, cv::Size(Params-&gt;winstride_x, Params-&gt;winstride_y), cv::Size(0,0), locations);
  }
  catch(...)
  {
      return -1;
  }
  //copy out the data into LabView
  error = DSSetHandleSize(FeatArrHdl, sizeof(int32_t) + ders.size()*sizeof(float));
  memcpy((*FeatArrHdl)-&gt;Arr, ders.data(), sizeof(float)*ders.size());
  (*FeatArrHdl)-&gt;dimSize = ders.size();
  
  return error;
  </code></pre>
  
  <p>}</p>
  
  <p>I am running this function with following parameters:</p>
  
  <p>Window size 32
  Block size 16
  Cell size 8
  Block stride 8</p>
  
  <p>Window stride 32</p>
  
  <p>the rest of parameters is default.</p>
  
  <p>I decided to include the look of the Mat object once constructed, I hope it can help.</p>
  
  <p>This is the BGRA constructed from user data. It is supposed to be 640*640 BGRA</p>
  
  <ul>
  <li>BGRAimg {flags=1124024344 dims=2 rows=640 ...}  cv::Mat
      flags   1124024344  int
      dims    2   int
      rows    640 int
      cols    640 int</li>
  <li>data    0x12250040 "e9%"    unsigned char *
          101 'e' unsigned char</li>
  <li>refcount    0x00000000  int *
          CXX0030: Error: expression cannot be evaluated  </li>
  <li>datastart   0x12250040 "e9%"    unsigned char *
          101 'e' unsigned char</li>
  <li>dataend 0x123e0040 ""   unsigned char *
          0   unsigned char</li>
  <li>datalimit   0x123e0040 ""   unsigned char *
          0   unsigned char</li>
  <li>allocator   0x00000000  cv::MatAllocator *
      __vfptr CXX0030: Error: expression cannot be evaluated  </li>
  <li>size    {p=0x0012f44c } cv::Mat::MSize</li>
  <li>p   0x0012f44c  int *
          640 int</li>
  <li>step    {p=0x0012f474 buf=0x0012f474 }  cv::Mat::MStep</li>
  <li>p   0x0012f474  unsigned int *
          2560    unsigned int</li>
  <li>buf 0x0012f474  unsigned int [2]
      [0] 2560    unsigned int
      [1] 4   unsigned int</li>
  </ul>
  
  <p>And the Grey image that enters the HOG descriptors calculator</p>
  
  <ul>
  <li>GREYimg {flags=1124024320 dims=2 rows=640 ...}  cv::Mat
      flags   1124024320  int
      dims    2   int
      rows    640 int
      cols    640 int</li>
  <li>refcount    0x0c867ff0  int *
          1   int</li>
  <li>dataend 0x0c867ff0 ""   unsigned char *
          1 ''    unsigned char</li>
  <li>datalimit   0x0c867ff0 ""   unsigned char *
          1 ''    unsigned char</li>
  <li>allocator   0x00000000  cv::MatAllocator *
      __vfptr CXX0030: Error: expression cannot be evaluated  </li>
  <li>size    {p=0x0012f40c } cv::Mat::MSize</li>
  <li>p   0x0012f40c  int *
          640 int</li>
  <li>step    {p=0x0012f434 buf=0x0012f434 }  cv::Mat::MStep</li>
  <li>p   0x0012f434  unsigned int *
          640 unsigned int</li>
  <li>buf 0x0012f434  unsigned int [2]
      [0] 640 unsigned int
      [1] 1   unsigned int</li>
  </ul>
  
  <p>I had to ommit the data and datastart fields, because unlike for the BGRA image MSVS actually shows some data in it.</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>changed multi-threaded for multi-threaded DLL in project properities, and the issue is gone.</p>
  
  <p>The problem persisted even if I was using code like this :</p>
  
  <pre><code>int dim = 32;
  BYTE *mydata = NULL;
  mydata = (BYTE*)malloc(sizeof(BYTE)*dim*dim);
  Mat img;
  img = Mat(Size(dim,dim), CV_8U, mydata, dim*sizeof(BYTE));
  </code></pre>
  
  <p>Might this indicate my code was not the cause, and this is somewhat opencv x windows runtime issue, or did I just hide the problem ?</p>
  
  <p><strong>UPDATE3</strong></p>
  
  <p>After reading something about microsoft runtime, I decided to check how was my opencv built, and it is using /MD, and I was building with /MT. I hope this was the cause.</p>