Matlab在MEX功能中崩溃

时间:2018-08-30 00:46:50

标签: c matlab pointers mex

我有一个用MATLAB编写的程序,我需要一个计算(代码中的snrf(beta(i),gk_sum,gk_length)),因此C需要发挥作用(通过MEX函数)。在与官方文档和我发现的一些示例进行了反复交流之后,我设法使整个过程工作了几次。但是,现在发生的是,当此计算即将发生时(在函数中),MATLAB崩溃了。 (如果我声明plhs[0]为矩阵-如示例所示-我的输出是一个充满零的矩阵(不正确)。)

这是我的MEX文件中的代码:

#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);
}

这是崩溃文件:

------------------------------------------------------------------------
          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

我最后应该注意到我正在使用MATLAB 2017b,并且我的编译器是MinGW64 v5.3.0。

1 个答案:

答案 0 :(得分:3)

您的代码中存在多个错误,其中大多数会导致段错误并杀死MATLAB。我建议您阅读(或重新阅读)有关mex文件的一些基本文档,例如Create C Source MEX File

根据文档,mxGetData仅用于获取指向非数字数据的指针,并返回指向void的指针。您的snrf函数需要双精度的指针,因此推测对mex文件的输入应该是标量。在这种情况下,您应该使用mxGetScalar,它返回一个双精度型,因此您可以将这些双精度型的地址传递给函数。您还应该使用mxIsScalar之类的功能来检查用户确实输入了标量。

在尝试使用plhs[0]访问指向数字数据的指针之前,还必须为mxGetPr分配内存。但就您而言,由于(至少我怀疑)您尝试输出标量,因此您可以使用mxCreateDoubleScalar

plhs[0] = mxCreateDoubleScalar(output_from_snrf)

但是,您的函数snrf不会创建/返回输出(也不会更改输入所指向的任何值),因此您需要查看一些基本的C教程以了解如何先这样做。