3

Below is some simple OpenCV code to create a frame from a video file and run the SURF feature detector and extractor on the frame. When I run this code on Linux and OSX it runs fine, however on windows I am getting a heap corruption error on the two commented lines.

VideoCapture capture(vidFilename.c_str());
Mat frame;
capture >> frame; 

SurfFeatureDetector *detector = new SurfFeatureDetector(minHessian);
vector<KeyPoint> frameKeypoints;
detector->detect(frame, frameKeypoints);
delete(detector); // Heap Corruption Detected

SurfDescriptorExtractor *extractor = new SurfDescriptorExtractor();
Mat frameDescriptors;
extractor->compute(frame, frameKeypoints, frameDescriptors);
delete(extractor); // Heap Corruption Detected

I have no clue what could cause this in the code. I am using VS 2010 to compile the code, is there something in VS that could cause this to happen?

Kazz
  • 33
  • 1
  • 4
  • 1
    More than likely, it is your program that is at fault, and not Visual Studio. What if you did this: `SurfFeatureDetector *detector = new SurfFeatureDetector(minHessian); delete(detector);` In other words, delete right after you allocate with "new". If your program no longer crashes, then inspect what detect does, as well as whether KeyPoint has proper copy semantics (if not, it shouldn't be placed in a vector). – PaulMcKenzie Apr 08 '14 at 18:25
  • Mixing release and debug? I mean you can not safely use Release DLLs in a debug app or Debug dlls in a release app. That is without isolating the allocations / deallocations to make sure that you do not free memory in a different heap than you allocated it. Release and Debug have independent heaps. – drescherjm Apr 08 '14 at 18:28
  • 2
    Just because you don't get a notice of heap corruption does not mean that it didn't happen. – Ed S. Apr 08 '14 at 18:38
  • Use smart pointers like `std::auto_ptr()` (http://www.cplusplus.com/reference/memory/auto_ptr/) instead.. This will make sure that the compiler automatically deletes the pointer once it goes out of scope. – scap3y Apr 08 '14 at 20:00
  • @scap3y I have tried a smart pointer on this and that just postpones the heap error until the object goes out of scope. – Kazz Apr 08 '14 at 20:04

1 Answers1

2

As mentioned above that you do not get any exception related to heap corruption does not mean that it did not happen.There would be problem in your code and not in VS or compiler.My previous post on the similar article would be useful here as well.

https://stackoverflow.com/a/22074401/2724703

Probably you should also try to run some dynamic tool(Valgrind) on your linux. There is high possibility that, you would find the same bug using Valgrind as well.

These dynamic tools would give you the root cause of these problem.

Community
  • 1
  • 1
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
  • Using Valgrind on Linux I am not getting any heap or memory errors. Just a couple "Conditional jump or move depends on uninitialized value(s)" and those are within the OpenCV library. – Kazz Apr 08 '14 at 19:53
  • 1
    @Kazz:Please understand those warning(uninitialized values) as they can lead to such problem.Try to understand from the perspective of who is the owner of that particular memory not the one who is using that(OpenCV). You may want to read my post http://stackoverflow.com/a/22658693/2724703 which explains about how to use Valgrind/GDB together to perform the live debugging of your program. – Mantosh Kumar Apr 09 '14 at 02:02
  • 2
    Turns out that the time(&tm) function needs to have the tm struct initialized to zero. Thanks for your help. – Kazz Apr 09 '14 at 16:20