5

Many functions in the latest release of OpenCV require the use of STL containers. I run into problems when trying to use them in a Matlab MEX file. I am compiling the MEX files from within Matlab. Both OpenCV and Matlab use the "/MD" flag which is "Multithreaded DLL" for code generation.

Compiler: MSVC++ 9.0 Matlab 2010a OpenCV latest from SVN, 2.11 I think.

The code I am using is very simple:

vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);

This compiles but crashes when run in a Matlab MEX file. The crash is within OpenCV in vector::resize. The old interface (without STL containers) works fine but is deprecated. How can I use STL containers between Matlab and OpenCV?

2
  • Does it crash whatever the input ? Jul 29, 2010 at 14:17
  • Yes, it seems to crash whatever the input.
    – Petter
    Aug 1, 2010 at 7:21

4 Answers 4

2

I fought with this very problem in the last two days. The problem is this:
libmex.dll (and a whole Matlab) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your OpenCV uses Microsoft.VC90.CRT (version=9.0.21022.8)

So you can use the previous version of VC (VS 2005 with SP1 as far as I know), or as a workaround, you can use gcc (MINGW) (in this case they use totally different stl, so they can't interfere).
I did the latter and it works, and it will work with the next versions of Matlab (hopefully).

1

A long time ago I had problems with Matlab <-> VS interop. It might be some microsoft visual c++ runtime library discrepancy. Check what runtime lib is required by matlab and what version does your visual studio have. I remember using Depends to get the dll dependencies for my program. Check your call stack after crashing (by attaching your msdev debugger) it might give you some hints.

It was a long time ago so I'm just giving hints of what I remember.

5
  • I haven't used Spy++ yet. I just made sure that the same compiler was used and that the same switches (/MD) was passed, which I though would be enough to have them use the same version.
    – Petter
    Jul 31, 2010 at 11:26
  • Sorry I said Spy++ it's actually Depends (Dependency walker). Check this out: msdn.microsoft.com/en-us/library/ms235265(VS.80).aspx
    – code-gijoe
    Aug 2, 2010 at 17:48
  • 2
    Check if it crashes inside a Matlab dll or inside a MS runtime dll (by attaching your debugger).
    – code-gijoe
    Aug 2, 2010 at 17:51
  • It crashes inside opencv_features2d211.dll when OpenCV tries to call vector::resize.
    – Petter
    Aug 3, 2010 at 8:19
  • Well, my best advice would be to compile OpenCV in debug so you can check why it's crashing. Have you tried giving the vector an initial size? Some big value.
    – code-gijoe
    Aug 3, 2010 at 13:46
1

I had a similar problem in the past few days, and was able to resolve the issue with some help from the friendly folks at MathWorks.

From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :

You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.

You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.

Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.

Following his advice, I removed the SECURE_SCL=0 option from the mex options file at

C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat

Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.

0

The data in a vector should still be stored as a single contiguous block

std::vector<int> data;
int *array = &data[0]; 
int *array = &data.front(); 

Should give you 'c' style pointers to the data, try passing these to matlab

see also: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?

1
  • You misunderstood my problem. The problem is not passing data to Matlab. It is the fact that the above code crashes.
    – Petter
    Jul 31, 2010 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.