Access violation writing location [closed]
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++)
{
contoursQuery.push_back(_contoursQuery[border][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++)
{
cont.push_back(contoursQuery[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.
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.
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
Sorry but on what part of the program should I clone the Mat image? I'm still getting the same error after cloning currentQuery.
I'd try changing the following (though I don't know if that will solve it...):
Are you sure that your application has writing rights on the device?
@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
The new error is also in the findContours function? Can you check the call stack?
@StevenPuttemans the error appears when running the code in Windows, so I don't think it is related to writing permissions
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.