Matlab 在 MEX 函数中崩溃

Posted

技术标签:

【中文标题】Matlab 在 MEX 函数中崩溃【英文标题】:Matlab crashes in MEX function 【发布时间】:2018-08-30 00:46:49 【问题描述】:

我有一个用 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。

【问题讨论】:

在 MATLAB 代码中,没有什么比计算更容易完成的了。您确定需要 MEX 文件吗? 这个计算的结果给出了一个非常大的数字,MATLAB 的精度不足以计算它。这弄乱了我的图表。 这个 C 代码计算它的精度与 MATLAB 完全相同,都使用双精度浮点数。 【参考方案1】:

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

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

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

plhs[0] = mxCreateDoubleScalar(output_from_snrf)

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

【讨论】:

以上是关于Matlab 在 MEX 函数中崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Mex 编译函数,用于计算集合交集和差异,不断崩溃

如何在从 Matlab 调用的 mex 函数中使用 Matlab 引擎

mex 文件崩溃,如何在 matlab 中使用 MATLAB_MEM_MGR?

Matlab MEX-函数副作用

防止 MEX 文件在 MATLAB 中崩溃

在matlab中编写一个非常基本的mex函数