MATLAB Answers

Dan P

Mex dynamic memory management issue with std::vector in linked external DLL; Segmentation error

Asked by Dan P
on 12 Jun 2011
Hi there,
I am trying to create a mex file that interfaces MATLAB with an external C++ library that communicates with some hardware. An imported library and precompiled DLL (.lib and .dll) are provided by the hardware vendor for my version of VC++ and I was able to implement them in C++ without any issue.
However, I ran into segmentation error at run time when the code is written as a mex(compiled with the same version of VC++). After some investigation with the VC++ debugger, the likely culprit seems to be the fact that one of the external dll functions returns the data type std::vector<string>, and probably tries to dynamically allocate memory for the vector container somewhere inside the function. I know that if I use std::vector in my own mex function, everything works fine, but I suspect that the mex header itself wraps the std::vector container in my own code for memory management(?), whereas it can't do the same for the pre-compiled .dll.
Now the question is: since I cannot modify the external .dll file and have no access to its source files, are there any ways to work with this external dll such that the dynamic memory becomes managed by MATLAB(perhaps a wrapper of some sort..?)...and thereby avoid the segmentation error and return the correct data? Or if my analysis is wrong please correct me too!
Please let me know if there are any ideas or hacks, thanks!
My system: Windows 7 SP1 32 bit, MATLAB 2009b, Visual C++ 2008 Pro.

  1 Comment

AFAIK, MATLAB memory management is only used for data created using the mxArray API (for eg. mxCreateDoubleMatrix) - so the std::vector returned by your DLL should not be affected by this unless you are somehow returning the pointer to the std::vector data back to MATLAB (or setting it on an mxArray). As requested by Jan, please provide more details.

Sign in to comment.

2 Answers

Answer by Philip Borghesani on 13 Jun 2011
 Accepted Answer

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.


Thanks for the answer! I can finally sleep soundly tonight(until another bug comes along).
Philip - thank you so very much. Your solution was the end of my 4 days of debuging memory leaks.

Sign in to comment.

Answer by Dan P
on 13 Jun 2011

Thanks everyone for the answers, especially Philip for his insight on the incompatibility and how to resolve it, which turns out to be spot on.
I removed the SECURE_SCL=0 option from the mex options file at
Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.


Sign in to comment.