I'm using OpenCV and CascadeClassifier::detectMultiScale
for facial detection. My problem is that it seems to cause memory corruption on the output vector<Rect>
. The vector is correctly filled with Rects, but it causes a crash when the vector is deallocated.
This only occurs when compiling a Debug build. The error message is a Debug Assertion Failed, which makes me wonder if there is a problem that also occurs in Release build, and the assert simply isn't checked.
Could this be a bug with OpenCV? Or is it just that I'm doing something wrong with how I handle my vectors?
The following code snippet shows an example code to reproduce the bug:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(array<System::String ^> ^args)
{
VideoCapture video(0);
Mat frame;
CascadeClassifier classifier("haarcascade_frontalface_default.xml");
while (waitKey(1000 / 30) != 'q')
{
video >> frame;
vector<Rect> faces;
classifier.detectMultiScale(frame, faces);
for (int i = 0; i < faces.size(); i++)
rectangle(frame, faces[i], Scalar(255, 255, 255));
imshow("Camera", frame);
} // <<< The crash occurs here when the faces vector is released
}
I get the following error message:
Debug Assertion Failed!
Program: MyProgram.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 892
Expression: is_block_type_valid(header->_block_use)
I had this same issue. I solved it by passing a dereferenced global pointer to the function.
I.e.
std::vector<cv::Rect>* faces = nullptr;
void init()
{
faces = new std::vector<cv::Rect>; //never call delete whatever you do
}
void findSomeFaces()
{
cascade->detectMultiScale(image_source, *faces);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With