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