Segmentation violation while running mex file

3 次查看(过去 30 天)
I have compiled a C-code file turboreg.c using gcc compiler on Linux to create turboreg.mexa64. The compilation works but when I execute the mex file, Matlab crashes with a segmentation violation. The stacktrace is shown below and the C file is attached (extension changed to .txt to be able to upload it). Any help regarding this problem would be greatly appreciated.
--------------------------------------------------------------------------------
Segmentation violation detected at Thu Mar 28 11:25:34 2019 +0100
--------------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Default Encoding : UTF-8
Deployed : false
GNU C Library : 2.17 stable
Graphics Driver : Unknown software
Java Version : Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Architecture : glnxa64
MATLAB Entitlement ID : 1833009
MATLAB Root : /cluster/apps/matlab/R2018a
MATLAB Version : 9.4.0.813654 (R2018a)
OpenGL : software
Operating System : "CentOS Linux release 7.5.1804 (Core) "
Process ID : 23221
Processor ID : x86 Family 6 Model 71 Stepping 1, GenuineIntel
Session Key : 4735a760-8118-4de0-b9d9-b7ba5186b8bf
Static TLS mitigation : Enabled: Full
Window System : No active display
Fault Count: 1
Abnormal termination
Register State (from fault):
RAX = 00002b36aff49060 RBX = 00002b36aff3f3e0
RCX = 00002b36a8000598 RDX = 0000000000000080
RSP = 00002b36a7ffb910 RBP = 00002b36a7ffbb60
RSI = 0000000000000000 RDI = 00002b37238ffba0
R8 = 00002b36a0014290 R9 = 0000000000009c60
R10 = 0000000000000007 R11 = 00002b36811ff192
R12 = 3e800000001c5f68 R13 = 00002b36a7ffbd30
R14 = 00002b36aff49060 R15 = 0000000000000000
RIP = 00002b3731a8d40b EFL = 0000000000010202
CS = 0033 FS = 0000 GS = 0000
Stack Trace (from fault):
[ 0] 0x00002b3731a8d40b /cluster/home/hluetcke/Data/Projects/Grewe_MovieAnalysis/MovieAnalysis/misc/turboreg/C/turboreg.mexa64+00005131 mexFunction+00000747
[ 1] 0x00002b3690f23080 bin/glnxa64/libmex.so+00413824
[ 2] 0x00002b3690f23447 bin/glnxa64/libmex.so+00414791
[ 3] 0x00002b3690f23f2b bin/glnxa64/libmex.so+00417579
[ 4] 0x00002b3690f0e30c bin/glnxa64/libmex.so+00328460
[ 5] 0x00002b368f1a42ad bin/glnxa64/libmwm_dispatcher.so+00979629 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000829
[ 6] 0x00002b368f1a4bae bin/glnxa64/libmwm_dispatcher.so+00981934 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000030
[ 7] 0x00002b3692013da1 bin/glnxa64/libmwm_lxe.so+12619169
[ 8] 0x00002b3692014982 bin/glnxa64/libmwm_lxe.so+12622210
[ 9] 0x00002b36920fce79 bin/glnxa64/libmwm_lxe.so+13573753
[ 10] 0x00002b369209e3e1 bin/glnxa64/libmwm_lxe.so+13186017
[ 11] 0x00002b36918a45a8 bin/glnxa64/libmwm_lxe.so+04822440
[ 12] 0x00002b36918a6cbc bin/glnxa64/libmwm_lxe.so+04832444
[ 13] 0x00002b36918a301d bin/glnxa64/libmwm_lxe.so+04816925
[ 14] 0x00002b369189cba1 bin/glnxa64/libmwm_lxe.so+04791201
[ 15] 0x00002b369189cdd9 bin/glnxa64/libmwm_lxe.so+04791769
[ 16] 0x00002b36918a2846 bin/glnxa64/libmwm_lxe.so+04814918
[ 17] 0x00002b36918a292f bin/glnxa64/libmwm_lxe.so+04815151
[ 18] 0x00002b36919d1503 bin/glnxa64/libmwm_lxe.so+06055171
[ 19] 0x00002b36919d4cf3 bin/glnxa64/libmwm_lxe.so+06069491
[ 20] 0x00002b3691ee4f6d bin/glnxa64/libmwm_lxe.so+11378541
[ 21] 0x00002b3692000fa1 bin/glnxa64/libmwm_lxe.so+12541857
[ 22] 0x00002b368f1a42ad bin/glnxa64/libmwm_dispatcher.so+00979629 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000829
[ 23] 0x00002b368f1a4bae bin/glnxa64/libmwm_dispatcher.so+00981934 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000030
[ 24] 0x00002b3692013da1 bin/glnxa64/libmwm_lxe.so+12619169
[ 25] 0x00002b3692014982 bin/glnxa64/libmwm_lxe.so+12622210
[ 26] 0x00002b36920fcfc9 bin/glnxa64/libmwm_lxe.so+13574089
[ 27] 0x00002b369209e431 bin/glnxa64/libmwm_lxe.so+13186097
[ 28] 0x00002b36918a45a8 bin/glnxa64/libmwm_lxe.so+04822440
[ 29] 0x00002b36918a6cbc bin/glnxa64/libmwm_lxe.so+04832444
[ 30] 0x00002b36918a301d bin/glnxa64/libmwm_lxe.so+04816925
[ 31] 0x00002b369189cba1 bin/glnxa64/libmwm_lxe.so+04791201
[ 32] 0x00002b369189cdd9 bin/glnxa64/libmwm_lxe.so+04791769
[ 33] 0x00002b36918a2846 bin/glnxa64/libmwm_lxe.so+04814918
[ 34] 0x00002b36918a292f bin/glnxa64/libmwm_lxe.so+04815151
[ 35] 0x00002b36919d1503 bin/glnxa64/libmwm_lxe.so+06055171
[ 36] 0x00002b36919d4cf3 bin/glnxa64/libmwm_lxe.so+06069491
[ 37] 0x00002b3691ee4f6d bin/glnxa64/libmwm_lxe.so+11378541
[ 38] 0x00002b3691e9260c bin/glnxa64/libmwm_lxe.so+11040268
[ 39] 0x00002b3691e99448 bin/glnxa64/libmwm_lxe.so+11068488
[ 40] 0x00002b3691e9ae22 bin/glnxa64/libmwm_lxe.so+11075106
[ 41] 0x00002b3691f28807 bin/glnxa64/libmwm_lxe.so+11655175
[ 42] 0x00002b3691f28aea bin/glnxa64/libmwm_lxe.so+11655914
[ 43] 0x00002b3690c9b91a bin/glnxa64/libmwbridge.so+00207130 _Z8mnParserv+00000874
[ 44] 0x00002b368ebd7bb8 bin/glnxa64/libmwmcr.so+00641976
[ 45] 0x00002b367afd0e9f bin/glnxa64/libmwmlutil.so+06524575 _ZNSt13__future_base13_State_baseV29_M_do_setEPSt8functionIFSt10unique_ptrINS_12_Result_baseENS3_8_DeleterEEvEEPb+00000031
[ 46] 0x00002b3679b20e70 /lib64/libpthread.so.0+00052848 pthread_once+00000080
[ 47] 0x00002b367afd1126 bin/glnxa64/libmwmlutil.so+06525222 _ZSt9call_onceIMNSt13__future_base13_State_baseV2EFvPSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEEPbEJPS1_S9_SA_EEvRSt9once_flagOT_DpOT0_+00000102
[ 48] 0x00002b368ebd79d3 bin/glnxa64/libmwmcr.so+00641491
[ 49] 0x00002b36766411a2 bin/glnxa64/libmwmvm.so+03367330 _ZN14cmddistributor15PackagedTaskIIP10invokeFuncIN7mwboost8functionIFvvEEEEENS2_10shared_ptrINS2_13unique_futureIDTclfp_EEEEEERKT_+00000082
[ 50] 0x00002b36766414e8 bin/glnxa64/libmwmvm.so+03368168 _ZNSt17_Function_handlerIFN7mwboost3anyEvEZN14cmddistributor15PackagedTaskIIP10createFuncINS0_8functionIFvvEEEEESt8functionIS2_ET_EUlvE_E9_M_invokeERKSt9_Any_data+00000024
[ 51] 0x00002b368e671e6c bin/glnxa64/libmwiqm.so+00867948 _ZN7mwboost6detail8function21function_obj_invoker0ISt8functionIFNS_3anyEvEES4_E6invokeERNS1_15function_bufferE+00000028
[ 52] 0x00002b368e67197f bin/glnxa64/libmwiqm.so+00866687 _ZN3iqm18PackagedTaskPlugin7executeEP15inWorkSpace_tagRN7mwboost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+00000447
[ 53] 0x00002b368e64fab1 bin/glnxa64/libmwiqm.so+00727729
[ 54] 0x00002b368e632ac8 bin/glnxa64/libmwiqm.so+00608968
[ 55] 0x00002b368e62d8bf bin/glnxa64/libmwiqm.so+00587967
[ 56] 0x00002b367751fa05 bin/glnxa64/libmwservices.so+03262981
[ 57] 0x00002b3677520ff2 bin/glnxa64/libmwservices.so+03268594
[ 58] 0x00002b36775218fb bin/glnxa64/libmwservices.so+03270907 _Z25svWS_ProcessPendingEventsiib+00000187
[ 59] 0x00002b368ebd8fc3 bin/glnxa64/libmwmcr.so+00647107
[ 60] 0x00002b368ebd96a4 bin/glnxa64/libmwmcr.so+00648868
[ 61] 0x00002b368ebd23f1 bin/glnxa64/libmwmcr.so+00619505
[ 62] 0x00002b3679b1be25 /lib64/libpthread.so.0+00032293
[ 63] 0x00002b36782a2bad /lib64/libc.so.6+01043373 clone+00000109
[ 64] 0x0000000000000000 <unknown-module>+00000000
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

采纳的回答

Jan
Jan 2019-3-28
编辑:Jan 2019-3-28
After some speculations, this might be (one of) the crashing point(s):
mwSize *dimsOut;
...
dimsOut[0] = 1;
dimsOut[1] = 1;
You forgot to allocate dimsOut, such that it is a "dangeling pointer". Use this instead:
mwSize dimsOut[2];
Fix the other problems mentioned here also, because C code is fragile.

更多回答(2 个)

James Tursa
James Tursa 2019-3-28
编辑:James Tursa 2019-3-28
The first thing I noticed is the code is not robust against an input that is not exactly as expected. E.g.,
  • You don't check that nrhs >= 5 before you start dereferencing the prhs[ ] items
  • You don't check that nlhs >= 2 before you assign a value to plhs[1]
  • You don't check that the data areas of the prhs[ ] items are not NULL before you dereference them
  • You don't check that a field name is present before you dereference it.
  • You don't check that field data areas are not NULL before you dereference them
Any of the above could easily crash MATLAB if violated. I would suggest you first put in checks for all of this and generate an error return if anything is amiss.
And this line, although it won't generate an error, is rather odd because you immediately forget the mxArray pointer and only save the data pointer:
imgOutMask = (float *)mxGetData(mxCreateNumericArray (2, dims, mxSINGLE_CLASS, mxREAL));
If you just want a data pointer to some float data, why go through all of the mxArray stuff? Why not just use mxMalloc?
And, for that matter, why are you dynamically allocating dims? Since it always contains two elements, why not just declare it as:
mwSize dims[2];

Jan
Jan 2019-3-28
编辑:Jan 2019-3-28
Prefer mxGetScalar instead of SmoothX=*mxGetPr(field_value);
Mixing integer types can cause serious troubles:
int nx;
nx = mxGetM(prhs[0]);
mxGetM replies a size_t, which is not necessarily an int.
[EDITED] Assumption about re-using myArray pointers removed.
  3 个评论
Jan
Jan 2019-3-28
@James: I've missinterpreted this line of the documentation:
If you want to assign the contents of pvalue to multiple fields, then use the mxDuplicateArray function to make copies of the data then call mxSetField on each copy.
The "multiple" is important.
Henry
Henry 2019-3-28
Dear James and Jan,
thank you both for your quick replies and suggestions. I will work through them and report back with my results.
Cheers, Henry

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by