Why is SetNumThreads causing a crash on exit??

asked 2014-09-18 05:13:11 -0600

manikandtan.ck gravatar image

updated 2014-09-19 03:38:12 -0600

When I use setNumThreads to more than 1, it causes the following error upon exiting application.

Unhandled exception at 0x77558519 (ntdll.dll) in test1_opencv_backgroundsubtraction.exe: 0xC000000D: An invalid parameter was passed to a service or function.

Upon breaking this is the last function call in stack:

VOID __cdecl __crtSetThreadpoolTimer(
  __inout   PTP_TIMER pti,
  __in_opt  PFILETIME pftDueTime,
  __in      DWORD msPeriod,
  __in_opt  DWORD msWindowLength)
{
    // use SetThreadpoolTimer if it is available (only on Vista+)...
    IFDYNAMICGETCACHEDFUNCTION(KERNEL32, PFNSETTHREADPOOLTIMER, SetThreadpoolTimer, pfSetThreadpoolTimer)
    {
        (*pfSetThreadpoolTimer)(pti, pftDueTime, msPeriod, msWindowLength);
    }

    // ...otherwise there is no fall back.
    return;
}

it stops at the return.

If I disable the setNumThreads, it uses around 8 threads and exits correctly. If it is set to 1, it exits correctly. What am I doing wrong?

Configuration:

Visual Studio Professional 2013 Update 3 x86 build Opencv 2.4.9 prebuilt binaries for vc12.

Full Code:

#include<opencv2/opencv.hpp>
#include <string>
#include<iostream>
#include <fstream>
#include<vector>
#include<time.h>
#include <windows.h>
#include <wchar.h>
using namespace cv;
int main(int argc, char *argv[])
{

    //setUseOptimized(true);
    setNumThreads(2);
    int features = -1;
    bool rt = checkHardwareSupport(features);
    //bool result = useOptimized();
    int nCores = getNumberOfCPUs();
    int nThreads = getNumThreads();



    const int nmixtures = 3;
    const bool bShadowDetection = true;
    const int history = 50;
    const int varThresholdGen = 20;
    time_t start, end;
    Mat element = getStructuringElement(MORPH_RECT,Size(7,7),Point(1, 1));
    int largest_area = 0;
    int largest_contour_index = 0;
    Rect bounding_rect = { 0, 0, 0, 0 };

    cv::BackgroundSubtractorMOG2 bg(history, nmixtures, bShadowDetection);
    bg.set("varThresholdGen", 20);


    //Start the clock
    time(&start);
    int counter = 0;
    std::vector<std::vector<cv::Point> > contours;
    cv::namedWindow("foreground", CV_WINDOW_KEEPRATIO);

    cv::Mat frame(1088, 2048, CV_8UC3);
    cv::Mat back(1088, 2048, CV_8UC3, cv::Scalar(0, 0, 0));
    cv::Mat fore(1088, 2048, CV_8UC1, cv::Scalar(0, 0, 0));
    cv::Mat fore1(1088, 2048, CV_8UC1, cv::Scalar(0, 0, 0));


    cv::Mat mask = cv::Mat::zeros(1088, 2048, CV_8UC1);
    //mask(Rect(300, 200, 1600, 600)) = 255;

    int linecount = 0;
    std::string line;
    std::ifstream infile("images2.txt");

    try
    {
        if (!infile.eof())
        {
                while (getline(infile, line))
            {
                //std::cout << linecount << ": " << line << '\n';//supposing '\n' to be line end

                linecount++;

                frame = cv::imread(line);


                long long e1 = getTickCount();


                resize(frame, frame, Size(frame.rows / 4, frame.cols / 4), CV_INTER_NN);
                    bg.operator ()(frame, fore);

                bg.getBackgroundImage(back);

                cv::erode(fore, fore, cv::Mat(), cv::Point(-1, -1), 1);

                cv::dilate(fore, fore, cv::Mat(), cv::Point(-1, -1), 1);

                vector<Vec4i> hierarchy;

                threshold(fore, fore, 128, 255, 0);

                cv::imshow("foreground", fore);

                cv::waitKey(1);

                            ++counter;

                long long e2 = getTickCount();
                float timetaken = (e2 - e1) / getTickFrequency();
                double fps = counter / timetaken;
                nThreads = getNumThreads();
                printf("FPS: %lf \n", fps);

            }
        }

            infile.close();
        nThreads = getNumThreads();


    }
    catch (cv::Exception& e)
    {
        const char* err_msg = e.what();
        std::cout << "exception caught: " << err_msg << std::endl;
    }

    return 0;
}

edit retag flag offensive close merge delete

Comments

This may be a Microsoft Visual C++ Runtime bug. To help investigate, you will need to post your Operating System version, the full stack trace, the version and build numbers of compiler and the C++ Runtime, and if necessary you may need to provide a minidump and a small code sample for reproducing the issue. This issue should be reported to Microsoft via http://connect.microsoft.com/

rwong gravatar imagerwong ( 2014-09-19 15:16:10 -0600 )edit