1

I'm having trouble getting classes/structs in OpenCV in a vector and leaving the function alive without any heap corruption. Any time I have a vector of native OpenCV objects open, the dreaded _crtIsValidHeapPointer error comes up during garbage collection.

Context: Trying to use ORB to compare matching points in a Object and Scene images. Using OpenCV 2.3.1 in C++.

Code example:

vector< KeyPoint > objImageKeypoints;
Mat objImageDescriptors;
OrbFeatureDetector detector = orbFeatureDetector( 1000 );

detector.detect( objImage, objImageKeypoints );

... // There's more, but I've commented out the rest
}

After I've passed the detect() method, there's no turning back. After that, it'll still give me trouble.

System blows up once I hit the end of the function. Am I missing something, or have I committed some cardinal sin of OpenCV by using vectors?

Cœur
  • 37,241
  • 25
  • 195
  • 267
rm -rf slash
  • 296
  • 3
  • 12
  • I switched IDE from VS11 to 10 Express, and that seemed to fix it. I'm going to chalk this up to OpenCV's infamously terrible setup, but I'll leave the question open in case a better answer manages to come through. – rm -rf slash Feb 02 '12 at 04:49
  • i have compiled my opencv source in vs2008 same error occurs how to solve? pls help – Prasaathviki Oct 20 '14 at 10:25
  • see this question [question]: http://stackoverflow.com/questions/30787348/heap-corruption-using-cvflannbasedmatcher-and-stdvector maybe that's your problem – jperezmartin Jun 17 '15 at 15:20
  • `All Configuratins -> General -> Platform Toolset -> Visual Studio 2010 (v100)` solved problem for me. – vkalit Dec 06 '15 at 00:46

4 Answers4

4

I had this problem when using Visual Studio 2012(version 11) with OpenCV2.4.3 originally build only for 2010 (version 10). I was using lib and dll for version 10. Than I build it for 2012 nad now it works without heap corruption error.

P.S.: Before it (when using built for version 10), I resized vector before used in opencv function.

VisionC
  • 106
  • 4
  • i have compiled my opencv source in vs2008 same error occurs how to solve? pls help – Prasaathviki Oct 20 '14 at 10:23
  • The above answer is correct. I had to reinstall opencv visual studio under the proper version and the error (along with many others) simply went away. – rm -rf slash Oct 20 '14 at 21:51
2

I was having this problem today, with the HoughLinesP function.

From searching the internet and looking at questions like these, I have finally found the answer. VisionC's answer here seems to be on the right track, but lacks a bit of explanation. I imagine you don't still need the answer anymore, but this might be helpful to others maybe.

What the problem is:

I'm no expert, but thanks to Michael's answer here, as I understand it, the Platform Toolset used to build OpenCV is v100 (i.e. the toolset used in Visual Studio 10). In Visual Studio 11 (2012), projects default to using the newer v110 Platform Toolset. (I haven't been able to find anything that says if new versions of OpenCV use v110.)

What seems to be happening is that you give an OpenCV function (which is using some v100 dll's) a vector, which the function then modifies. When the vector is cleaned up by your program (using v110 dll's) there's an incompatibility, due to it being modified by OpenCV, which causes the error.

Sorry that's so vague, but as I said I don't really understand the ins-and-outs of it.

How to fix it:

Anyway, answers I've found on this didn't really have good solutions. They'd talk about rebuilding OpenCV and other tedious things. As you've discovered, going back to VS2010 fixes the issue, but having to convert the project and go back to the old IDE isn't too much fun. And if you're using C++/CLI like I am, you don't get IntelliSense in VS2010. :(

All you need to do to is go to your project properties in VS2012. Make sure the configuration is set to "All Configurations". Then, under "Configuration Properties->General->Platform Toolset" choose "Visual Studio 2010 (v100)". This should fix the problem, and in the future you can easily change the setting back if needed.

As described here though, you do still need to have VS 2010 installed (or the Windows Platform SDK) to be able to do this.

Community
  • 1
  • 1
Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33
2

i was have the same problem in OpenCV3 and i found the answer. the problem in my case that i use Libs from Release and not debug you need to make sure to use the correct Libs/Dlls when you are in debug please make sure to use Debug Libs/Dlls.

  • I had the same problem with 3.1 libs, after hours and hours of searching I finally encountered your answer that solved it. Thanks! – Drakarah Feb 18 '16 at 16:44
1

Since the OpenCV documentation also uses vector<KeyPoint> (in the read and write functions in the first code block) this shouldn't be the source of your problem.

Probably whatever else you are doing in that function causes the error.

sth
  • 222,467
  • 53
  • 283
  • 367
  • I've added the detector.detect() line. After that, in the example and in the code, nothing else happens. I've commented out the rest. Still blows up. – rm -rf slash Feb 02 '12 at 04:29