views:

221

answers:

1

I have been looking in to writing my own implementation of Haar Cascaded face detection for some time now, and have begun with diving in to the OpenCV 2.0 implementation.

Right out of the box, running in debug mode, Visual Studio breaks on cvhaar.cpp:1518, informing me:

Run-Time Check Failure #2 - Stack aound the variable seq_thread was corrupted.

It seems odd to me that OpenCV ships with a simple array out-of-bounds problem. Running the release works without any problems, but I suspect that it is merely not performing the check and the array is exceeding the bounds.

Why am I receiving this error message? Is it a bug in OpenCV?

A: 

A little debugging revealed the culprit, I believe. I "fixed" it, but this all still seems odd to me.

An array of size CV_MAX_THREADS is created on cvhaar.cpp:868:

CvSeq* seq_thread[CV_MAX_THREADS] = {0};

On line 918 it proceeds to specify max_threads:

max_threads = cvGetNumThreads();

In various places, seq_thread is looped using the following for statement:

for( i = 0; i < max_threads; i++ ) {
    CvSeq* s = seq_thread[i];
    // ...
}

However, cxmisc.h:108 declares CV_MAX_THREADS:

#define CV_MAX_THREADS 1

Hence, the declaration of seq_thread must never be allowed to exceed size 1, yet cvGetNumThreads() returns 2 (I assume this reflects the number of cores in my machine).

I resolved the problem by adding the following simple little statement:

if (max_threads > CV_MAX_THREADS) max_threads = CV_MAX_THREADS;

Does any of this make sense?

Paul Lammertsma