C++ 释放无效指针错误

C++ 释放无效指针错误,c++,opencv,C++,Opencv,我有以下for循环: for (size_t matchIdx = 0; matchIdx < matcherTypes.size(); matchIdx++) { std::vector< DMatchVector > matches12; std::vector< DMatchVector > matches21; try { poi.knnMatch(descriptorsInterior, descriptorsCar, cfg.ge

我有以下for循环:

for (size_t matchIdx = 0; matchIdx < matcherTypes.size(); matchIdx++)
{
  std::vector< DMatchVector > matches12;
  std::vector< DMatchVector > matches21;
  try
  {
    poi.knnMatch(descriptorsInterior, descriptorsCar, cfg.getPOIMatchKNN(),
                 matcherTypes[matchIdx], matches21);
    poi.knnMatch(descriptorsCar, descriptorsInterior, cfg.getPOIMatchKNN(),
                 matcherTypes[matchIdx], matches12);
  }
  catch (MyPOIException& mpoie)
  {
    std::cerr << mpoie.what() << std::endl;
  }

  DMatchVector matches;
  filterDMatch(matches12, matches21, matches);

  ElectPOI elPOI;
  std::vector< size_t > cntrVec(matches.size(), 0);
  elPOI.counterPOIs(matches, cntrVec);

  KeyPointVector filteredKPs;
  elPOI.thresholdKeyPoints(1, kpInteriorVec, cntrVec, filteredKPs);

  std::cout << "keypoints found:   " << kpInteriorVec.size() << std::endl
            << "keypoints matched: " << matches.size() << std::endl;


  cv::Mat tmpMat;
  cv::drawKeypoints(interiorImage, filteredKPs, tmpMat);
  cv::imshow("filtered keypoints", tmpMat);
  cv::waitKey();

} // for each matcher type
void POI::knnMatch(const cv::Mat& queryDescriptorsIn, const cv::Mat& trainDescriptorsIn,
                   int knnIn, const std::string& matcherTypeIn,
                   std::vector< DMatchVector >& matchesOut)
{
  std::vector<DMatchVector> result;

  cv::Ptr< cv::DescriptorMatcher > descriptorMatcher
                                          = cv::DescriptorMatcher::create(matcherTypeIn);
  if (descriptorMatcher.empty())
  {
    std::string msg = "Matcher of type " + matcherTypeIn + " was not created!";
    throw MyPOIException(msg);
  }
  descriptorMatcher->knnMatch(queryDescriptorsIn, trainDescriptorsIn, result, knnIn);

  matchesOut = result;
}
我不明白它为什么会崩溃,并出现以下错误:

*** Error in `/home/me/projects/prj/build/prj': free(): invalid pointer: 0x000000000110dda2 ***
事实上,在显示图像之前,循环的每一步都会崩溃。有什么建议吗?我做错了什么


编辑

瓦尔格林说:

==10329== Invalid read of size 8
==10329==    at 0x605479: ElectPOI::counterPOIs(std::vector<cv::DMatch, std::allocator<cv::DMatch> > const&, std::vector<unsigned long, std::allocator<unsigned long> >&) (CElectPOI.cpp:12)
==10329==    by 0x5D8A87: main (main.cpp:199)
==10329==  Address 0xc74ec60 is not stack'd, malloc'd or (recently) free'd

valgrind: m_mallocfree.c:303 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 449, hi = 450.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata.  If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away.  Please try that before reporting this as a bug.
==10329==大小为8的无效读取
==10329==0x605479:ElectPOI::countropois(std::vector const&,std::vector&)(CElectPOI.cpp:12)
==10329==0x5D8A87:main(main.cpp:199)
==10329==地址0xc74ec60不是堆栈、malloc或(最近)空闲
valgrind:m_mallocfree.c:303(按原样获取):断言'bszB_lo==bszB_hi'失败。
valgrind:堆块低/高大小不匹配:低=449,高=450。
这可能是由于您的程序错误地写入了
堆块结束并损坏堆元数据。如果你修好了
Memcheck报告的写入无效,此断言将失败
可能会走开。请在将此报告为bug之前尝试一下。
功能如下:

void ElectPOI::counterPOIs(const DMatchVector& matchesIn, std::vector< size_t >& cntrVecInOut)
{
  for (size_t i = 0; i < matchesIn.size(); i++)
  {
    int qryIdx = matchesIn[i].queryIdx;    
    cntrVecInOut[qryIdx]++;  // line 12
  }
}
void electropoi::countropois(const-DMatchVector&matchesIn,std::vector和cntrVecInOut)
{
对于(size_t i=0;i
您粘贴的代码的哪一行出现了错误?您的代码可能写入了对象的边界,或者在某个地方损坏了内存。尝试使用valgrind这样的工具来帮助发现问题。我已经提到过:“事实上,在显示图像之前,循环的每一步都会崩溃”,但这不是一条精确的线。在for循环的第二次迭代结束时。PS我已经编辑了一个问题:调试器在崩溃时显示了什么调用堆栈?@theDarksideofmon
int qryIdx=matchesIn[I].queryIdx;cntrVecInOut[qryIdx]+
是否
qryIdx
在向量的范围内?你为什么不确定这是真的?我可以很容易地看出这是一个不在向量范围内的数字。