MATLAB crashes when a C++ S-Function uses a statically-linked library code for logging to a file (R2013b, Win64)

2 次查看(过去 30 天)
Hello,
I'm experiencing a problem in MATLAB R2013b running on a Win64 system. I have a C++ S-Function that uses methods of a 3rd-party class for logging some infos to a file. When the tracer class is built externally and statically-linked to the S-Function, MATLAB/Simulink crashes when the S-Function calls the class method that writes to the log file. A snapshot of the crash-dump is discussed below. On the other hand, when the tracer class and its methods are declared inside the S-Function code, all works just fine!
I set-up a minimal example that reproduces the problem. I've extended the sfun_counter_cpp.cpp example for use with a tracer class. You can find a dummy Simulink model that uses the S-Function here.
This setup makes MATLAB/Simulink to crash. The compressed archive contains an external library with the tracer class (simple_tracer) and the S-Function code that uses the tracer (fw-simulink). Follow the steps into the README.txt to run the example. The make_sf.m script contains the commands I used to build the S-Function and link it to the simple_tracer library. Remark: run the example will crash your current instance of MATLAB/Simulink!
Finally, the setup here uses exactly the same tracer code above but hard-coded inside the S-Function. In this case all works fine. Follow the indications in the README.txt to run the example.
This is a snapshot of the crash-dump (first setup: sfun->external_lib). You can see that mdlOutputs() calls TextTrace::probe()(respectively [7] and [6]), and then TextTrace::probe() calls the std::operator<< of the std::ostream (resp, [5] and [4]). The program execution crash is apparently due to an access violation on a lock of std::basic_filebuf ([2]) in c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\fstream.
Stack Trace (from fault):
[ 0] 0x00000000777ce4e4 C:\Windows\SYSTEM32\ntdll.dll+00320740 RtlDeNormalizeProcessParams+00001444
[ 1] 0x00000000777ce40b C:\Windows\SYSTEM32\ntdll.dll+00320523 RtlDeNormalizeProcessParams+00001227
[ 2] 0x000007fef48ca87f C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00043135 std::basic_filebuf<char,std::char_traits<char> >::_Lock+00000063
[ 3] 0x000007fef48c9834 C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00038964 std::basic_ostream<char,std::char_traits<char> >::_Sentry_base::_Sentry_base+00000164
[ 4] 0x000007fef48c987b C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00039035 std::basic_ostream<char,std::char_traits<char> >::sentry::sentry+00000059
[ 5] 0x000007fef48c8dc7 C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00036295 std::operator<<<std::char_traits<char> >+00000263
[ 6] 0x000007fef48c8c6e C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00035950 TextTrace::probe+00000062
[ 7] 0x000007fef48c668a C:\Users\matteo\Documents\MATLAB\fw_fails\fw-simulink\libs\sfun_counter_tracer.mexw64+00026250 mdlOutputs+00000186
These below are the details of my machine (from the crash-dump):
Configuration:
Crash Decoding : Disabled
Default Encoding : windows-1252
MATLAB Architecture: win64
MATLAB Root : C:\Program Files\MATLAB\R2013b
MATLAB Version : 8.2.0.701 (R2013b)
Operating System : Microsoft Windows 7
Processor ID : x86 Family 6 Model 42 Stepping 7, GenuineIntel
Virtual Machine : Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 6.1 (Build 7601: Service Pack 1)
Any suggestion on how to fix this issue? Is this a MATLAB bug (should I fill a bug report)?
Thanks!
-- Matteo

回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by