5

这篇文章的参考资料:

我成功地实现了一个 Matlab/C++ 接口,基于 [1] 中提出的方法。无论如何,我在尝试将系统与 Matlab 并行计算一起使用时遇到了麻烦。

在 MEX 接口中尝试在 matlab 句柄和 C++ 指针之间进行转换时,发生在我身上的是分段错误。

为了更清楚,我将回顾 [1] 中提出的结构。系统中有三个文件,采用这种通信方案:

[myInterface.m] <--> [myMexInterface.cpp] <--> [myClass.cpp]

在哪里

  • myInterface.m 是一个 matlab 类
  • myMexInterface.cpp 是一个 C++ (mex) 函数
  • myClass.cpp 是一个 C++ (mex) 类

该系统的使用分为两个阶段:

  • 构造:创建一个matlab对象myInterface。这会导致调用 myMexInterface.mexa64,从而调用 C++ myClass 对象的创建。myClass 的 C++ 指针通过 myMexInterface.mexa64 发送回 myInterface,存储它以供进一步使用。特别是,myMexInterface.mexa64 将myClass 的 C++ 指针转换为 matlab 句柄。
  • 使用 matlab 中的 C++ 类:myInterface 向客户端提供方法,这些方法通过 myMexInterface.mexa64 传递,调用对象 myClass 上的函数。在此阶段,myMexInterface.mexa64 需要在构造阶段从 myInterface 存储的句柄才能调用正确 C++ 对象上的函数。当然,在这个阶段 myMexInterface.mexa64 会进行反向转换,从 Matlab 句柄到 C++ 指针。

在我的实现中,它在单线程中工作,在从句柄到指针的转换过程中会出现分段错误错误。特别是,我想关注 myMexInterface.cpp。要执行的命令通过字符串作为第一个参数传递,而可能(对于第二阶段操作)第二个参数是相对于与该接口关联的 C++ 对象的 matlab 句柄。

#include "mex.h"
#include "class_handle.hpp"

    CLASS void myMexInterface(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  if (!strcmp("new", cmd)) {
    // ...
    plhs[0] = convertPtr2Mat<myClass>(new myClass());
  }
  if (!strcmp("delete", cmd)) {
    // ...
    destroyObject<myClass>(prhs[1]);   
  }
  // Get the class instance pointer from the second input
  cout << " trying to convert handle..."; 
  myClass *myClass_instanceAddress = convertMat2Ptr<myClass>(prhs[1]); // SEGMENTATION FAULT ON MULTI CORE!!!
  cout << "   success in handle conversion. \n"; 
  if (!strcmp("aFunction", cmd)) {
    myClass_instanceAddress->aFunction();
  }
  // .. other functions
}

生成段错误的函数 convertMat2Pt 来自包含 class_handle.hpp,这是 [1] 中提出的解决方案的一部分。特别是那个class_handle.hpp中的函数,其中段错误是convertMat2HandlePtr:

template<class base> class class_handle

// ...

template<class base> inline class_handle<base> *convertMat2HandlePtr(const mxArray *in)
{
  if (mxGetNumberOfElements(in) != 1 || mxGetClassID(in) != mxUINT64_CLASS || mxIsComplex(in))
      mexErrMsgTxt("Input must be a real uint64 scalar.");

  std::cout << "class_handle: trying to cast \n";
  class_handle<base> *ptr = reinterpret_cast<class_handle<base> *>(*((uint64_t *)mxGetData(in))); // SEGMENTATION FAULT ON MULTI CORE!!!
  if (!ptr->isValid())
    mexErrMsgTxt("Handle not valid.");
  return ptr;
}

template<class base> inline base *convertMat2Ptr(const mxArray *in)
{
  return convertMat2HandlePtr<base>(in)->ptr();
}

// ...

实际上我不清楚那个演员阵容中到底发生了什么,所以我无法进行更深入的分析。我可以想象的是,由于某种原因,Matlab 并行计算与之前创建的 C++ 对象产生了不一致。

生成段错误的matlab客户端函数如下:

myInterface = myMexInterface();
matlabpool open local 1   
out = myModelInterf.aFunction()
disp('                               Now starting parfor ***');
parfor i = 1:1
  out = myModelInterf.aFunction()
end

注意,为了简化情况,我在matlabpool中只打开了一个worker,parfor只执行了一个循环:反正我还是有错误。当然,如果没有 parfor 块,即使多次调用 aFunction(),也不会出错。

我在命令窗口中获得的是:

Create interface with CPP handle: 139698584223104
Starting matlabpool using the 'local' profile ... connected to 1 workers.
    trying to convert handle...class_handle: trying to cast 
success in handle conversion. 

out =

    3

                  Now starting parfor ***
Save interface with CPP handle: 139698584223104
Save interface with CPP handle: 139698584223104
Create interface with CPP handle: 0
Load interface with CPP handle: 139698584223104
    trying to convert handle...class_handle: trying to cast 

------------------------------------------------------------------------
      Segmentation violation detected at Wed Jan 30 15:00:47 2013
------------------------------------------------------------------------

Configuration:
  Crash Decoding  : Disabled
  Current Visual  : None
  Default Encoding: UTF-8
  GNU C Library   : 2.15 stable
  MATLAB Root     : /usr/local/MATLAB/R2012b
  MATLAB Version  : 8.0.0.783 (R2012b)
  Operating System: Linux 3.2.0-31-generic #50-Ubuntu SMP Fri Sep 7 16:16:45 UTC 2012 x86_64
  Processor ID    : x86 Family 6 Model 42 Stepping 7, GenuineIntel
  Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
  Window System   : No active display

Fault Count: 1


Abnormal termination:
Segmentation violation

Register State (from fault):
  RAX = 00007fedbcba74a0  RBX = 00007fee2afa0fe0
  RCX = 0000000000000006  RDX = 0000000000000060
  RSP = 00007fee2afa0520  RBP = 00007fee2afa09f0
  RSI = 0000000000000000  RDI = 00007fee2b817a50

  R8 = 00007fee2afa09af   R9 = 00007fedbcdb9208
  R10 = 00007fee2afa0200  R11 = 00007fee3d57ba00
  R12 = 0000000000000002  R13 = 00007f0e1c7cfd80
  R14 = 00007fee2afa0dd0  R15 = 00007fee2afa0f20

  RIP = 00007fedfc00a8e4  EFL = 0000000000010206

  CS = 0033   FS = 0000   GS = 0000

Stack Trace (from fault):
[  0] 0x00007fee3f5e31de    /usr/local/MATLAB/R2012b/bin/glnxa64/libmwfl.so+00516574 _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+000158
[  1] 0x00007fee3f5e44b2    /usr/local/MATLAB/R2012b/bin/glnxa64/libmwfl.so+00521394
[  2] 0x00007fee3f5e5ffe    /usr/local/MATLAB/R2012b/bin/glnxa64/libmwfl.so+00528382 _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextE+000174
[  3] 0x00007fee3e8d2093   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00557203 _ZN2fl4diag13terminate_logEPKcPK8ucontext+000067
[  4] 0x00007fee3e8ceb9d   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00543645
[  5] 0x00007fee3e8d0835   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00550965
[  6] 0x00007fee3e8d0a55   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00551509
[  7] 0x00007fee3e8d10fe   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00553214
[  8] 0x00007fee3e8d1295   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00553621
[  9] 0x00007fee3cdc8cb0              /lib/x86_64-linux-gnu/libpthread.so.0+00064688
[ 10] 0x00007fedfc00a8e4 /home/gwala/Documents/mexInitConfig/mex_torque_profile.mexa64+00010468 mexFunction+002116
[ 11] 0x00007fee355b269a     /usr/local/MATLAB/R2012b/bin/glnxa64/libmex.so+00112282 mexRunMexFile+000090
[ 12] 0x00007fee355ae4e9     /usr/local/MATLAB/R2012b/bin/glnxa64/libmex.so+00095465
[ 13] 0x00007fee355af33c     /usr/local/MATLAB/R2012b/bin/glnxa64/libmex.so+00099132
[ 14] 0x00007fee3e620a4b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00596555 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000539
[ 15] 0x00007fee3deb4e56 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02264662
[ 16] 0x00007fee3de651c6 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01937862
[ 17] 0x00007fee3de69ab4 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01956532
[ 18] 0x00007fee3de660d3 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01941715
[ 19] 0x00007fee3de66ed7 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01945303
[ 20] 0x00007fee3ded2760 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02385760
[ 21] 0x00007fee3e620a4b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00596555 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000539
[ 22] 0x00007fee35b73538  /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcos.so+01574200
[ 23] 0x00007fee35b15232  /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcos.so+01188402
[ 24] 0x00007fee35b154ce  /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcos.so+01189070
[ 25] 0x00007fee35b1723c  /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcos.so+01196604
[ 26] 0x00007fee35bfc9c7  /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcos.so+02136519
[ 27] 0x00007fee3e5d6431 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00291889 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+000529
[ 28] 0x00007fee3deb4933 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02263347
[ 29] 0x00007fee3dec40d8 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02326744
[ 30] 0x00007fee3dec7038 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02338872
[ 31] 0x00007fee3de6ab18 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01960728
[ 32] 0x00007fee3de660d3 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01941715
[ 33] 0x00007fee3de66ed7 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01945303
[ 34] 0x00007fee3ded2760 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02385760
[ 35] 0x00007fee3e62053b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00595259 _ZN8Mfh_file11dispatch_fhEP20_mdUnknown_workspaceiPP11mxArray_tagiS4_+000555
[ 36] 0x00007fee3e5e3057 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00344151 _Z30callViamdMxarrayFunctionHandlePviPP11mxArray_tagiS2_+000039
[ 37] 0x00007fee3de3e143 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01777987
[ 38] 0x00007fee3de3f07d /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01781885
[ 39] 0x00007fee3de3f78b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01783691
[ 40] 0x00007fee3de42759 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01795929
[ 41] 0x00007fee3de6922b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01954347
[ 42] 0x00007fee3de660d3 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01941715
[ 43] 0x00007fee3de66ed7 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01945303
[ 44] 0x00007fee3ded2760 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02385760
[ 45] 0x00007fee3e62053b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00595259 _ZN8Mfh_file11dispatch_fhEP20_mdUnknown_workspaceiPP11mxArray_tagiS4_+000555
[ 46] 0x00007fee3e5e3057 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00344151 _Z30callViamdMxarrayFunctionHandlePviPP11mxArray_tagiS2_+000039
[ 47] 0x00007fee3ddf38b5 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01472693 inFullFevalFcn+001045
[ 48] 0x00007fee3e5e66ba /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00358074 _ZN11Mfh_builtin11dispatch_mfEiPP11mxArray_tagiS2_+000074
[ 49] 0x00007fee3e5d6431 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00291889 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+000529
[ 50] 0x00007fee3e0a1140 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+04280640
[ 51] 0x00007fee3e0a197a /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+04282746
[ 52] 0x00007fee3e0a24ea /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+04285674
[ 53] 0x00007fee3df054cd /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02593997
[ 54] 0x00007fee3df30d22 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02772258
[ 55] 0x00007fee3df30e4f /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02772559
[ 56] 0x00007fee3e04db30 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+03939120
[ 57] 0x00007fee3de69fec /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01957868
[ 58] 0x00007fee3de660d3 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01941715
[ 59] 0x00007fee3de66ed7 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01945303
[ 60] 0x00007fee3ded2760 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02385760
[ 61] 0x00007fee3e620a4b /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_dispatcher.so+00596555 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000539
[ 62] 0x00007fee3de9388f /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02128015
[ 63] 0x00007fee3de92c69 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+02124905
[ 64] 0x00007fee3ddf129c /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01462940 inCallFcnWithTrap+000092
[ 65] 0x00007fee3de57bfb /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01883131
[ 66] 0x00007fee3ddf0168 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwm_interpreter.so+01458536 _Z28inCallFcnWithTrapInDesiredWSiPP11mxArray_tagiS1_PKcbP15inWorkSpace_tag+000104
[ 67] 0x00007fee36365b09   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwiqm.so+02878217 _ZN3iqm15BaseFEvalPlugin7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000457
[ 68] 0x00007fedff8a992d /usr/local/MATLAB/R2012b/bin/glnxa64/libnativejmi.so+00674093 _ZN9nativejmi14JmiFEvalPlugin7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000173
[ 69] 0x00007fedff8d6c45 /usr/local/MATLAB/R2012b/bin/glnxa64/libnativejmi.so+00859205 _ZN3mcr3mvm27McrSwappingIqmPluginAdapterIN9nativejmi14JmiFEvalPluginEE7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000629
[ 70] 0x00007fee3633bbfa   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwiqm.so+02706426
[ 71] 0x00007fee3632d594   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwiqm.so+02647444
[ 72] 0x00007fee357d7ccd /usr/local/MATLAB/R2012b/bin/glnxa64/libmwbridge.so+00122061 _Z10ioReadLinebP8_IO_FILERKN5boost8optionalIKP15inWorkSpace_tagEEb+000429
[ 73] 0x00007fee357d8354 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwbridge.so+00123732
[ 74] 0x00007fee357dd71d /usr/local/MATLAB/R2012b/bin/glnxa64/libmwbridge.so+00145181
[ 75] 0x00007fee357dd81e /usr/local/MATLAB/R2012b/bin/glnxa64/libmwbridge.so+00145438
[ 76] 0x00007fee357ddf07 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwbridge.so+00147207 _Z8mnParserv+000631
[ 77] 0x00007fee3e8b7472   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00447602 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+000034
[ 78] 0x00007fee3e895b69   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00310121
[ 79] 0x00007fee3e895d48   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00310600
[ 80] 0x00007fee3eeabf73 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+00999283 _ZN10eventqueue18UserEventQueueImpl5flushEv+000371
[ 81] 0x00007fee3eeac695 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01001109 _ZN10eventqueue8ReadPipeEib+000053
[ 82] 0x00007fee3eeab321 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+00996129 _ZN10eventqueue18UserEventQueueImpl9selectFcnEb+000353
[ 83] 0x00007fee3284fa65   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwuix.so+00518757
[ 84] 0x00007fee3ef45a11 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01628689 _ZSt8for_eachIN9__gnu_cxx17__normal_iteratorIPN5boost8weak_ptrIN4sysq10ws_ppeHookEEESt6vectorIS6_SaIS6_EEEENS4_8during_FIS6_NS2_10shared_ptrIS5_EEEEET0_T_SH_SG_+000081
[ 85] 0x00007fee3ef46aeb /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01633003 _ZN4sysq12ppe_for_eachINS_8during_FIN5boost8weak_ptrINS_10ws_ppeHookEEENS2_10shared_ptrIS4_EEEEEET_RKS9_+000251
[ 86] 0x00007fee3ef445a2 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01623458 _ZN4sysq19ppePollingDuringFcnEb+000114
[ 87] 0x00007fee3ef44969 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01624425 _ZN4sysq11ppeMainLoopEiib+000121
[ 88] 0x00007fee3ef44b08 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01624840 _ZN4sysq11ppeLoopIfOKEiib+000152
[ 89] 0x00007fee3ef44c63 /usr/local/MATLAB/R2012b/bin/glnxa64/libmwservices.so+01625187 _ZN4sysq20processPendingEventsEiib+000147
[ 90] 0x00007fee3e896664   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00312932
[ 91] 0x00007fee3e896b3c   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00314172
[ 92] 0x00007fee3e890592   /usr/local/MATLAB/R2012b/bin/glnxa64/libmwmcr.so+00288146
[ 93] 0x00007fee3cdc0e9a              /lib/x86_64-linux-gnu/libpthread.so.0+00032410
[ 94] 0x00007fee3caedcbd                    /lib/x86_64-linux-gnu/libc.so.6+00998589 clone+000109


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.

If this problem is reproducible, please submit a Service Request via:
    http://www.mathworks.com/support/contact_us/

A technical support engineer might contact you with further information.

Thank you for your help.** This crash report has been saved to disk as /home/gwala/matlab_crash_dump.11705-1 **

class_handle: trying to cast 
Destroy object 0x7f0e1e45f090
class_handle: trying to cast 
Deleted myclass with handle: 139698584223104
Error using parallel_function (line 589)
The session that parfor is using has shut down.

可以看出,接口是正确创建的,然后 aFunction 正确返回值 3 为 'out' (这甚至适用于多次调用)。然后 parfor 循环开始,众所周知 matlab 对象已保存(实际上我不清楚为什么它会保存两次,但即使在命令行窗口中,当我保存 myInterface 对象时也会发生这种情况)。最后创建一个新的 myInterface,句柄为 0,句柄恢复为正确的先前值。Altought,对 aFunctions 的调用失败。

我最后报告了 myInterface matlab 类:

classdef myInterface < handle
    properties (SetAccess = private, Transient=true)
    objectHandle; % Handle to the underlying C++ class instance
    end

    methods(Static=true)
    function obj = loadobj(this)     
        obj = myInterface();
        obj.objectHandle = this.objectHandle;
        disp(['Load interface with CPP handle: ' num2str(this.objectHandle)]);
    end
    end

    methods

    function obj = saveobj(this)
        obj.objectHandle = this.objectHandle;
        disp(['Save interface with CPP handle: ' num2str(this.objectHandle)]);
    end     


    %% Constructor - Create a new C++ class instance 
    function this = myInterface(varargin)
        if (size(varargin) == 0) % constructor with no arguments. Used in load/save operations
        this.objectHandle = 0;
        else % constructor with normal arguments
        this.objectHandle = myMexInterface('new', varargin{:});
        end
        disp(['Create interface with CPP handle: ' num2str(this.objectHandle)]);
    end

    %% Destructor - Destroy the C++ class instance
    function delete(this)
        myMexInterface('delete', this.objectHandle);
        disp(['Deleted myclass with handle: ' num2str(this.objectHandle)]);
    end

      %% aFunction
    function varargout = aFunction(this, varargin)
        [varargout{1:nargout}] =  myMexInterface('aFunction', this.objectHandle,varargin{:});
    end
    end
end

请注意,正如 [2] 中所建议的,我包含了 loadobj、saveobj 函数,并且该类具有“Transient”属性以实现加载/保存操作。

希望在某人的帮助下,我希望这篇文章可以帮助某人。

问候, 加布里埃尔·瓜兰迪

4

1 回答 1

5

并行计算工具箱工作者是独立的 MATLAB 进程——它们甚至可能在不同的机器上运行,因此它们具有独立的地址空间。如果我理解正确,您将指向在您的 MATLAB 客户端上有效的对象的指针传递给工作人员,并尝试在那里取消引用该指针。

如果您明确使用共享内存区域在客户端和工作人员之间进行通信,这可能会起作用。例如,请参阅此文件交换贡献: http: //www.mathworks.co.uk/matlabcentral/fileexchange/28572-sharedmatrix

正如我在上面提到的一个问题的解决方案中提出的那样,替代方案是支持更充分地保存到磁盘 - 在这种情况下,这可能意味着将内容从 C++ 对象中拉出到 MATLAB 结构中,并支持重新创建来自该 MATLAB 结构的 C++ 对象。

于 2013-01-31T07:32:30.813 回答