Matlab crashes in MEX function

Matlab crashes in MEX function



I have a program written in MATLAB and I need a calculation (snrf(beta(i),gk_sum,gk_length) in the code), so C needs to come into play (via a MEX function). After some back and forth with the official documentation and some examples that I have found, I managed to make the whole thing work a couple of times. However, what happens now is that when this calculation is about to happen (in the function), MATLAB crashes. (If I declare plhs[0] to be a matrix - as the examples suggest - my output is a matrix full of zeros (not correct)).


snrf(beta(i),gk_sum,gk_length)


plhs[0]



This is the code in my MEX file:


#include "mex.h"
#include <math.h>
void snrf(double *beta, double *gk_sum, double *gk_length)

double sinr, sinrdb;

sinr = -(*beta)*log(exp(-(*gk_sum)/(*beta)/(*gk_length)));
sinrdb = 10*log10(sinr);


/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs,
int nrhs, const mxArray *prhs)

double *beta;
double *gk_sum;
double *gk_length;
double *output;

if(nrhs != 3) mexErrMsgTxt("Must have 3 input arguments.");
if(nlhs != 1) mexErrMsgTxt("Too many output arguments.");

beta = mxGetData(prhs[0]);
gk_sum = mxGetData(prhs[1]);
gk_length = mxGetData(prhs[2]);

//plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); /*Creates a 1x1 matrix*/

output = mxGetPr(plhs[0]); /* Outuput */
snrf(beta, gk_sum, gk_length);



This is the crash file:


------------------------------------------------------------------------
Access violation detected at Thu Aug 30 00:35:56 2018
------------------------------------------------------------------------

Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Current Graphics Driver: Unknown hardware
Default Encoding : windows-1252
Deployed : false
Graphics card 1 : Intel Corporation ( 0x8086 ) Intel(R) HD Graphics 4000 Version 10.18.10.4358 (2015-12-21)
Graphics card 2 : NVIDIA ( 0x10de ) NVIDIA GeForce 610M Version 23.21.13.9135 (2018-3-23)
Host Name : DESKTOP-SMECHLA
MATLAB Architecture : win64
MATLAB Entitlement ID: 6257193
MATLAB Root : C:Program FilesMATLABR2017b
MATLAB Version : 9.3.0.713579 (R2017b)
OpenGL : hardware
Operating System : Microsoft Windows 10 Pro
Processor ID : x86 Family 6 Model 58 Stepping 9, GenuineIntel
Virtual Machine : Java 1.8.0_121-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 10.0 (Build 17134)

Fault Count: 1


Abnormal termination:
Access violation

Register State (from fault):
RAX = 000000000000001a RBX = 0000000026749980
RCX = 0000000000000000 RDX = 0000000000000068
RSP = 00000000043f9ec8 RBP = 00000000043f9ef0
RSI = 0000000026748b40 RDI = 0000000026748cc0

R8 = 0000e2d497fd0909 R9 = 00007ffa9e6f0000
R10 = 000000006480b240 R11 = 00000000043f9e70
R12 = 00000000043f9f50 R13 = 000000000000127f
R14 = 00000000c7c70220 R15 = 0000000000000001

RIP = 0000000180089354 EFL = 00010202

CS = 0033 FS = 0053 GS = 002b

Stack Trace (from fault):
[ 0] 0x0000000180089354 binwin64pgolibmx.dll+00562004 mxGetPr+00000000
[ 1] 0x00000000047b15b6 C:UsersKrazatorDesktopC_calcsnrf.mexw64+00005558 mexFunction+00000175
[ 2] 0x00000000fc60234a binwin64libmex.dll+00140106 mexRunMexFile+00000314
[ 3] 0x00000000fc600d22 binwin64libmex.dll+00134434 mexFeature_mexver+00002146
[ 4] 0x00000000fc5ffab7 binwin64libmex.dll+00129719 mexUnlock+00028455
[ 5] 0x00000000155dca93 binwin64pgom_dispatcher.dll+00117395 Mfh_file::dispatch_fh_impl+00000835
[ 6] 0x00000000155dc73e binwin64pgom_dispatcher.dll+00116542 Mfh_file::dispatch_fh+00000062
[ 7] 0x00000000155ca8d8 binwin64pgom_dispatcher.dll+00043224 Mfunction_handle::dispatch+00001032
[ 8] 0x0000000016249899 binwin64pgom_lxe.dll+00235673
[ 9] 0x000000001624b3a6 binwin64pgom_lxe.dll+00242598
[ 10] 0x000000001624bfb3 binwin64pgom_lxe.dll+00245683
[ 11] 0x000000001624dff5 binwin64pgom_lxe.dll+00253941
[ 12] 0x000000001624d44f binwin64pgom_lxe.dll+00250959
[ 13] 0x000000001624d822 binwin64pgom_lxe.dll+00251938
[ 14] 0x000000001631331b binwin64pgom_lxe.dll+01061659 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00604503
[ 15] 0x000000001631ad46 binwin64pgom_lxe.dll+01092934 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00635778
[ 16] 0x000000001631a570 binwin64pgom_lxe.dll+01090928 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00633772
[ 17] 0x000000001623fc91 binwin64pgom_lxe.dll+00195729
[ 18] 0x000000001623f906 binwin64pgom_lxe.dll+00194822
[ 19] 0x000000001623f925 binwin64pgom_lxe.dll+00194853
[ 20] 0x00000000155dca93 binwin64pgom_dispatcher.dll+00117395 Mfh_file::dispatch_fh_impl+00000835
[ 21] 0x00000000155dc73e binwin64pgom_dispatcher.dll+00116542 Mfh_file::dispatch_fh+00000062
[ 22] 0x00000000155ca8d8 binwin64pgom_dispatcher.dll+00043224 Mfunction_handle::dispatch+00001032
[ 23] 0x000000001624794e binwin64pgom_lxe.dll+00227662
[ 24] 0x0000000016244571 binwin64pgom_lxe.dll+00214385
[ 25] 0x000000001624b3a6 binwin64pgom_lxe.dll+00242598
[ 26] 0x000000001624bfb3 binwin64pgom_lxe.dll+00245683
[ 27] 0x000000001624dff5 binwin64pgom_lxe.dll+00253941
[ 28] 0x000000001624d44f binwin64pgom_lxe.dll+00250959
[ 29] 0x000000001624d822 binwin64pgom_lxe.dll+00251938
[ 30] 0x000000001631331b binwin64pgom_lxe.dll+01061659 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00604503
[ 31] 0x000000001631ad46 binwin64pgom_lxe.dll+01092934 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00635778
[ 32] 0x000000001631a570 binwin64pgom_lxe.dll+01090928 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00633772
[ 33] 0x00000000162aa4d6 binwin64pgom_lxe.dll+00632022 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00174866
[ 34] 0x00000000162a9ccd binwin64pgom_lxe.dll+00629965 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00172809
[ 35] 0x00000000162a9be6 binwin64pgom_lxe.dll+00629734 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00172578
[ 36] 0x00000000162a35a5 binwin64pgom_lxe.dll+00603557 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00146401
[ 37] 0x00000000162a3532 binwin64pgom_lxe.dll+00603442 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00146286
[ 38] 0x00000000162a71d5 binwin64pgom_lxe.dll+00618965 boost::serialization::singleton<boost::archive::detail::pointer_oserializer<boost::archive::binaryTerm_oarchive,MathWorks::lxe::MatlabIrTree> >::get_instance+00161809
[ 39] 0x0000000015725b63 binwin64pgom_interpreter.dll+00416611 inEvalCmdWithLocalReturn+00000063
[ 40] 0x00000000fb60de26 binwin64libmwbridge.dll+00122406 mnParser+00001254
[ 41] 0x00000000153abdb1 binwin64mcr.dll+00245169 mcr::runtime::setInterpreterThreadSingletonToCurrent+00029793
[ 42] 0x00000000153aace5 binwin64mcr.dll+00240869 mcr::runtime::setInterpreterThreadSingletonToCurrent+00025493
[ 43] 0x00000000153aad53 binwin64mcr.dll+00240979 mcr::runtime::setInterpreterThreadSingletonToCurrent+00025603
[ 44] 0x00000000153ab6e1 binwin64mcr.dll+00243425 mcr::runtime::setInterpreterThreadSingletonToCurrent+00028049
[ 45] 0x00000000fd02cc77 binwin64iqm.dll+00642167 iqm::UserEvalPlugin::pre+00028951
[ 46] 0x00000000fd039cfc binwin64iqm.dll+00695548 iqm::UserEvalPlugin::pre+00082332
[ 47] 0x00000000fd02737f binwin64iqm.dll+00619391 iqm::UserEvalPlugin::pre+00006175
[ 48] 0x00000000fd02cc16 binwin64iqm.dll+00642070 iqm::UserEvalPlugin::pre+00028854
[ 49] 0x00000000fd027863 binwin64iqm.dll+00620643 iqm::UserEvalPlugin::pre+00007427
[ 50] 0x00000000fd03c8b6 binwin64iqm.dll+00706742 iqm::UserEvalPlugin::pre+00093526
[ 51] 0x00000000fd0080f7 binwin64iqm.dll+00491767 iqm::PackagedTaskPlugin::PackagedTaskPlugin+00000759
[ 52] 0x00000000fd0088bf binwin64iqm.dll+00493759 iqm::PackagedTaskPlugin::execute+00000879
[ 53] 0x00000000fd00817d binwin64iqm.dll+00491901 iqm::PackagedTaskPlugin::PackagedTaskPlugin+00000893
[ 54] 0x00000000fd008708 binwin64iqm.dll+00493320 iqm::PackagedTaskPlugin::execute+00000440
[ 55] 0x00000000fcfdbd3a binwin64iqm.dll+00310586 iqm::Iqm::setupIqmFcnPtrs+00079802
[ 56] 0x00000000fcfdbc06 binwin64iqm.dll+00310278 iqm::Iqm::setupIqmFcnPtrs+00079494
[ 57] 0x00000000fcfbf5be binwin64iqm.dll+00193982 iqm::Iqm::deliver+00004046
[ 58] 0x00000000fcfc0545 binwin64iqm.dll+00197957 iqm::Iqm::deliver+00008021
[ 59] 0x00000001001237c1 binwin64libmwservices.dll+01259457 services::system_events::PpeDispatchHook::dispatchOne+00021505
[ 60] 0x0000000100128663 binwin64libmwservices.dll+01279587 sysq::addProcessPendingEventsUnitTestHook+00002211
[ 61] 0x0000000100128850 binwin64libmwservices.dll+01280080 sysq::addProcessPendingEventsUnitTestHook+00002704
[ 62] 0x0000000100129c26 binwin64libmwservices.dll+01285158 sysq::getCondition+00003462
[ 63] 0x000000010012ac66 binwin64libmwservices.dll+01289318 svWS_ProcessPendingEvents+00000230
[ 64] 0x00000000153ac244 binwin64mcr.dll+00246340 mcr::runtime::setInterpreterThreadSingletonToCurrent+00030964
[ 65] 0x00000000153ac964 binwin64mcr.dll+00248164 mcr::runtime::setInterpreterThreadSingletonToCurrent+00032788
[ 66] 0x00000000153a2762 binwin64mcr.dll+00206690 mcr_process_events+00008818
[ 67] 0x00000000152423c5 binwin64MVMLocal.dll+00271301 mvm_server::inproc::LocalFactory::terminate+00088005
[ 68] 0x00000000fa957669 binwin64mvm.dll+01209961 mvm::detail::initLocalMvmHack+00000569
[ 69] 0x00000000fa957e2b binwin64mvm.dll+01211947 mvm::detail::SessionImpl::privateSession+00000555
[ 70] 0x00000000fa958051 binwin64mvm.dll+01212497 mvm::detail::SessionImpl::privateSession+00001105
[ 71] 0x0000000140007833 binwin64MATLAB.exe+00030771
[ 72] 0x000000014000863f binwin64MATLAB.exe+00034367
[ 73] 0x00007ffaa92a3034 C:WINDOWSSystem32KERNEL32.DLL+00077876 BaseThreadInitThunk+00000020
[ 74] 0x00007ffaa99c1431 C:WINDOWSSYSTEM32ntdll.dll+00463921 RtlUserThreadStart+00000033



I should finally note that I am using MATLAB 2017b and my compiler is MinGW64 v5.3.0.





There is nothing in that calculation that cannot be done more easily in MATLAB code. Are you sure you need a MEX-file?
– Cris Luengo
Aug 30 at 4:03





The result of this calculation gives a very large number and MATLAB's precision isn't enough to calculate it. That messes me up my graphs.
– Spiros Krazator
Aug 30 at 11:44





This C code calculates this with exactly the same precision as MATLAB, both use double-precision floating points.
– Cris Luengo
Aug 30 at 12:50




1 Answer
1



There are multiple errors in your code, most of which will cause seg-faults and kill MATLAB. I'd suggest you read (or re-read) some of the basic documentation on mex files, such as Create C Source MEX File.



As per the doc, mxGetData is only used to get a pointer to non-numeric data, and returns a pointer to a void. Your snrf function requires pointers to doubles, so presumably the inputs to the mex file are expected to be a scalars. In this case you should be using mxGetScalar, which returns a double, so you would pass the address of these doubles to your function. You should also be using functions like mxIsScalar to check that the user did indeed input scalars.


snrf



You also must allocate memory for plhs[0] before you try to access the pointer to the numeric data using mxGetPr. But in your case, since you are (at least I suspect) trying to output a scalar, you can just use mxCreateDoubleScalar


plhs[0]


mxGetPr


plhs[0] = mxCreateDoubleScalar(output_from_snrf)



But, your function snrf doesn't create/return an output (nor does it change any of the values pointed to by the inputs), so you need to look at some basic C tutorials to see how to do that first.


snrf



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế