Heap corruption on Windows but not Linux - c++

Below is some simple OpenCV code to create a frame from a video file and run the SURF feature detector and extractor on the frame. When I run this code on Linux and OSX it runs fine, however on windows I am getting a heap corruption error on the two commented lines.
VideoCapture capture(vidFilename.c_str());
Mat frame;
capture >> frame;
SurfFeatureDetector *detector = new SurfFeatureDetector(minHessian);
vector<KeyPoint> frameKeypoints;
detector->detect(frame, frameKeypoints);
delete(detector); // Heap Corruption Detected
SurfDescriptorExtractor *extractor = new SurfDescriptorExtractor();
Mat frameDescriptors;
extractor->compute(frame, frameKeypoints, frameDescriptors);
delete(extractor); // Heap Corruption Detected
I have no clue what could cause this in the code. I am using VS 2010 to compile the code, is there something in VS that could cause this to happen?

As mentioned above that you do not get any exception related to heap corruption does not mean that it did not happen.There would be problem in your code and not in VS or compiler.My previous post on the similar article would be useful here as well.
https://stackoverflow.com/a/22074401/2724703
Probably you should also try to run some dynamic tool(Valgrind) on your linux. There is high possibility that, you would find the same bug using Valgrind as well.
These dynamic tools would give you the root cause of these problem.

Related

Opencv with cuda GpuMat::create() call causes access violation

I compiled (debug and release) opencv 4.1.0 with contrib modues and enabled cuda.
I'm trying to create a GpuMat and upload host data from cv::Mat.
This works properly when running in debug mode. However when I run in release mode (even when turning off optimizations) I get an access violation in the GpuMat::create() function (gpu_mat.cu line 174):
bool allocSuccess = allocator->allocate(this, rows, cols, esz);
"Exception thrown: read access violation.
this->allocator was 0x21A."
My code for creating the GpuMat:
cv::Mat raw = cv::Mat(inImg->height, inImg->width, CV_16UC1, inImg->host_data);
g_inImg.create(inImg->height, inImg->width, CV_16UC1);
The crash is only happens in Release mode.
Do anyone has a clue what could be the reason?
Thank you in advance.

malloc error while using Tesseract with OpenCL option enabled

I have compiled Tesseract 3.04.00 with the OpenCL option enabled. While trying to extract text from an image using GetUTF8Text(), there is a malloc error, a memory leak I suppose.
I found a patch for a memory leak error that was previously added, however, the version I have compiled already has the patch added. I am not sure as to why the memory leak has occurred.
This is the output I am getting:
[DS] Profile read from file (tesseract_opencl_profile_devices.dat).
[DS] Device[1] 1:Intel(R) Core(TM) i5-4250U CPU # 1.30GHz score is 14049349632.000000
[DS] Device[2] 1:HD Graphics 5000 score is 14049349632.000000
[DS] Device[3] 0:(null) score is 21474836480.000000
[DS] Selected Device[2]: "HD Graphics 5000" (OpenCL)
ACP(15114,0x7fff795bf300) malloc: *** mach_vm_map(size=1125865547108352) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Has anyone faced this problem before? How do I fix this?
I'm not familiar with Tesseract, but I suspect the patch that you referred was for a different issues.
Looking in the output details, It looks like you are using an apple computer. Please take a look at link below which contains some 'how to' for installing and using Tesseract on Mac OS X:
https://ryanfb.github.io/etc/2015/03/18/experimenting_with_opencl_for_tesseract.html
Hope this is useful to fix the issue.
Anyway, the error "can't allocate region" means that there is no memory space left. Indeed has been required a huge quantity of memory (size=1125865547108352, about 1.126 Petabyte). To figure out what is really happening, you should profile the code using a profiling tool like gdb (indeed the error message says "set a breakpoint in malloc_error_break to debug"), or at least upload a little program that can be used to reproduce the issue.
You ran out of memory. (error code=3) "can't allocate region" means malloc tried to allocate more memory than was available..
Maybe you can try to restrict recognition to a sub-rectangle of the image - call SetRectangle(left, top, width, height) after SetImage. Each SetRectangle clears the recogntion results so multiple rectangles can be recognized with the same image. E.g.
api->SetRectangle(30, 86, 590, 100);
Without seeing your code, I'd guess either you are not destroying objects and releasing memory or there is a bug with that version. To check if it's the former you can use a memory leak detection tool/library, to check if it is the later you can debug it or just try using a different version..

Opencv cvThreshold bug

Hello I don t know if I am doing something wrong or not but when I do the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_8U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
everything works fine then when I try to use unsigned short values:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
my program crashes... I use opencv 2.4.2 I think this could maybe be a bug in opencv.
Somehow if I try the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16S,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
it doesn t crash anymore
It is not crashing. It is throwing an exception and since you are not catching it, your program is aborting.
OpenCV Thresholding does not support 16U.
The supported ones are: 8U, 16S and 32F
See the OpenCV documentation for function threshold. The first parameter is 8 or 32 bit image
P.S. In crash message you should see the reason for crashing

Application crashes on equalizeHist of OpenCV

My MFC app runs various API from OpenCV2. Everything else is working fine. But when my program runs
cv::Mat result;
cv::equalizeHist(m_cvImage,result);
I get following runtime exception.
Unhandled exception at 0x7727fbae in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x0029e944..
"C:\slave\WinInstallerMegaPack\src\opencv\modules\imgproc\src\histogram.cpp:2430: error: (-215) CV_ARE_SIZES_EQ(src, dst) && CV_ARE_TYPES_EQ(src, dst) && CV_MAT_TYPE(src->type) == CV_8UC1"
According to debugger, the exception was thrown in the middle of processing (about 40%) the image in equalizeHist. Is there anything I need to do? FYI: I am using binary OpenCV provided by its web site.
UPDATE:
I've resolved this issue by converting images to gray-level before equalizing it. I just didn't know
the function only works with gray-level image
images that look like gray-level can be non-gray.
I imagine the problem you are encountering is that m_cvImage is a 3-channel image. So, you need to convert it to a grayscale image before you can call equalizeHist.
cvtColor(m_cvImage, m_cvImage, CV_BGR2GRAY);
cv::Mat result;
cv::equalizeHist(m_cvImage, result);
Also, have a look at the EqualizeHist_Demo.cpp tutorial sample to see how it is used.

Memory leak using cvCopy

I have a memory leak problem using the cvCopy function of OpenCV. If I comment that line it's all ok. If not the memory raises until the system crashes..
I've found this interesting article about OpenCV memory leaks: http://www.andol.info/hci/963.htm but if I comment the line:
targetImage = cvCreateImage( ....
I get another problem because it says that I'm passing a null pointer.
..... //other code (here we are inside a loop
cvSetImageROI(&tmpimag,TargetRect);
targetImage = cvCreateImage( cvSize(TargetRect.width, TargetRect.height), tmpimag.depth, tmpimag.nChannels );
cvCopy(&tmpimag,targetImage);
cvResetImageROI(&tmpimag); // release image ROI
....//other code
For what I can tell based on your little snippet of code, the mem leak might be your fault.
On each iteration of the loop you are creating/allocating a new image with cvCreateImage(), but I don't see you releasing it (check cvReleaseImage()). Therefore, after each iteration, more and more memory is allocated generating a genuine mem leak.
EDIT:
cvResetImageROI(&tmpimag); does not release an image, it just resets the ROI information previously set. You still need to cvReleaseImage(&tmpimag).
are you releasing targetImage each time the loop iterates?