Why does MATLAB crash when my MEX file is linked to a lib file containing STL classes?

3 views (last 30 days)
I have compiled a static .lib file outside of MATLAB, which contains classes/functions that use standard STL libraries. When I link to them from my mex file compiled with Visual Studio, MATLAB crashes during the call to this mex file. Either that, or completly incorrect data is being returned that makes it appear like the STL class constructors are not being called correctly.

Accepted Answer

Doug Hull
Doug Hull on 13 Jan 2011
MATLAB mex files are compiled with Visual Studio are compiled with the '_SECURE_SCL=0' flag. This flag changes the structure of all STL classes, and this different structure is incompatable with the STL classes compiled without this flag(default setting). The fix to this is to compile the .lib file with the '_SECURE_SCL=0' flag.
  1 Comment
Dan
Dan on 5 Oct 2011
Awesome! I just encountered this same issue. Thankfully I got lucky w/ my Google search and discovered your answer. Thanks!

Sign in to comment.

More Answers (1)

Uriel
Uriel on 3 Jun 2012
I have the same problem of crashes in stl structures in mex, but in Linux. My mex crashed in the destructor of an std::vector<int>. I could not find this _SECURE_SCL flag under a Linux installation of Matlab. In the mexopts.sh file (nor in any other file in /opt/matlab/bin) the value _SECURE_SCL does not exist. So, the question becomes: 1) Can stl be used in mex's under Linux? 2) If so, then how to avoid these crashes?

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!