Access violation writing location [closed]

asked 2015-11-22 22:52:15 -0600

thevinci gravatar image

updated 2015-11-23 06:46:10 -0600

I want to develop a sketch matching game using OpenCV. What I am trying to do is match an image and user's drawing using the Shape Context matching. I can successfully compile and run my project but when I call the findContour function the program will crash and will produce First-chance exception at 0x5C78DE7A (msvcp120d.dll) in DTPproject.exe: 0xC0000005: Access violation writing location 0x00000010 error.

here is my code

vector<cv::Point> CComputeDistance::simpleContour(const Mat& currentQuery, int n = 300)
    vector<vector<Point> > _contoursQuery;
    vector <Point> contoursQuery;
    findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
    for (size_t border = 0; border<_contoursQuery.size(); border++)
        for (size_t p = 0; p<_contoursQuery[border].size(); p++)

    // In case actual number of points is less than n
    int dummy = 0;
    for (int add = (int)contoursQuery.size() - 1; add<n; add++)
        contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values

    // Uniformly sampling
    random_shuffle(contoursQuery.begin(), contoursQuery.end());
    vector<Point> cont;
    for (int i = 0; i<n; i++)

    return cont;

void CComputeDistance::computeShapeDistance(Mat image, Mat drawing, float &result)
    Ptr <ShapeContextDistanceExtractor> mySC = createShapeContextDistanceExtractor();

    vector<cv::Point> a = simpleContour(image);
    vector<cv::Point> b = simpleContour(drawing);

    result = mySC->computeDistance(a, b);

void CComputeDistance::loopCompute(Mat image, Mat drawing, float &sum)
    int p = 20;
    for (int i = 0; i < 5; i++)
        float shapeDistance;
        computeShapeDistance(image, drawing, shapeDistance);

        sum = sum + shapeDistance;
        p = p + 20;

what I am really trying to achieve is get the value of "sum" after calling the method loopCompute in which it iterates 5x in loop.

Here is the code that call will the method

CComputeDistance::loopCompute( image, drawing, sum);

Im using visual studio 2013 community and this is what the local variable looks like after the break on debug mode.

image description

The program above runs perfectly if I run it in android but it fails on windows. I think it has something to do with the configuration of Visual Studio to OpenCV.

Any ideas?

EDIT: Here's what the CALL STACK looks like in Visual Studio. VISUAL STUDIO

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-10 14:36:18.226605



findContours "eats" your input image and modifies it. I don't know then if it can handle a const Mat. Try cloning the input image

LorenaGdL gravatar imageLorenaGdL ( 2015-11-23 02:23:05 -0600 )edit

Sorry but on what part of the program should I clone the Mat image? I'm still getting the same error after cloning currentQuery.

thevinci gravatar imagethevinci ( 2015-11-23 04:05:31 -0600 )edit

I'd try changing the following (though I don't know if that will solve it...):

vector<cv::Point> CComputeDistance::simpleContour(Mat& currentQuery, int n = 300)  //no more const Mat
computeShapeDistance(image.clone(), drawing.clone(), shapeDistance);  //in the loop, clone images if necessary
LorenaGdL gravatar imageLorenaGdL ( 2015-11-23 04:20:54 -0600 )edit

Are you sure that your application has writing rights on the device?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-23 04:54:05 -0600 )edit

@LorenaGdL - i tried your solution. I still have an error but this time its

First-chance exception at 0x67A60004 (opencv_world300d.dll) in DTPproject.exe: 0xC0000005: Access violation reading location 0x00000008.

@StevenPuttemans - I'm developing a cocos2d-x game for android. heres a link to the cocos2d-x website

thevinci gravatar imagethevinci ( 2015-11-23 06:22:26 -0600 )edit

The new error is also in the findContours function? Can you check the call stack?

LorenaGdL gravatar imageLorenaGdL ( 2015-11-23 06:27:28 -0600 )edit

@StevenPuttemans the error appears when running the code in Windows, so I don't think it is related to writing permissions

LorenaGdL gravatar imageLorenaGdL ( 2015-11-23 06:41:37 -0600 )edit

I can't reproduce the issue with Visual Studio 2013 Ultimate in Win7 x64. In fact, I tried with the first version using const Mat and it succeded too.

LorenaGdL gravatar imageLorenaGdL ( 2015-11-23 07:25:13 -0600 )edit