0

I am currently looking at computer vision and try to use Hough-Transform to find some lines. The operation it self is working, however even minimal sample code produces deallocation errors.

#include <iostream>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    int size[] = {100, 100};
    Mat img(1, size, CV_8U);
    img  = cv::Scalar(255);
    vector<Vec4i> lines;
    HoughLinesP(img, lines, 1, CV_PI/180, 100, 30, 5);

    cout << lines.size() << endl;
}

Note that everything is working if I turn lines into *lines, create the vector on the heap and don't delete it. I cannot see any problem with the code and it is taken straight from the OpenCV example.

The exception occurs in

msvcr110d.dll!_CrtIsValidHeapPointer(const void * pUserData) Line 2036  C++

I compile and run this from VS 2012 64bit-Version, and the executable is also 64bit. OS is Windows 7, 64bit

Mene
  • 3,739
  • 21
  • 40
  • Exact same problem. i have found a regression unit test in the OpenCV codebase that gives HoughLinesP a cv::Mat instead of a vector of vectors of ints. Maybe that's a clue? http://code.opencv.org/projects/opencv/repository/revisions/9908ff33dec00402a3793b87ce4c4087080141b6/entry/modules/imgproc/test/test_houghLines.cpp i'm poking around the code right now myself. Any help appreciated. – Doodloo Jan 14 '14 at 17:45
  • I haven't investigated it any further so far and just lived with it. However, it seems to be working with 2.4.8 in 32bit. I havn't set up a 64bit environment for my current project, but I propably will, once there is a bit room for "should-haves" in my schedule. I'll let you know what happens. – Mene Jan 16 '14 at 22:15

3 Answers3

1

Your application might be running in Release mode, try running it in Debug.
(I'm suggesting this because its using the debug DLL, msvcr110d.dll).

I came across this question, which you might find useful as well.

Hope that helps, let me know how it goes!

Community
  • 1
  • 1
crazylpfan
  • 1,038
  • 7
  • 9
  • I've checked it, but it's not the case. It runs in debug mode and links to all the *d.lib files. According to Dependency Walker my Executable uses OPENCV_CORE244D.DLL and they are both using MSVCP110D.DLL and MSVCR110D.DLL – Mene Mar 07 '13 at 23:34
1

This problem is solved by either one of those two options:

  • Linking your application against the same CRT as your OpenCV library version does,

  • Recompiling OpenCV and disabling the static CRT linking.

This is because the HoughLinesP allocates memory within the "lines" object passed by reference, but at the end of your function calling HoughLinesP, your application will de-allocate the memory. So with different CRT, this gets problematic.

Doodloo
  • 869
  • 5
  • 18
0
vector<Vec4i> lines; 
line.reserve(1000); //create enough place for lines

Doodlo:

This is because the HoughLinesP allocates memory within the "lines" object passed by reference,...

If you reserve enough lines place, then won't be memory problem because the function won't create memory field only using the reserved places.

Newd
  • 2,174
  • 2
  • 17
  • 31