我按照这里给出的解决方案: Passing pointer argument in MATLAB to a C-DLL function foo(char**)
我添加一个通过tcp socket接收的字符串,这是简化示例
C-DLL:
void test(char **u1, char **y1)
{
std::string recvString("Test Output"); //this string change
*y1 = (char*) mxRealloc(*y1, recvString.size()+1); //get memory
*y1 = strdup(recvString.c_str()); //copy
}
MATLAB:
loadlibrary('rriMonitor.dll', 'rriMonitor.h');
vpu = libpointer('stringPtrPtr',{'test input'});
vpv = libpointer('stringPtrPtr',{''});
calllib('rriMonitor','test',vpu,vpv);
vpv.value{1} %gives correct string 'Test Output'
%clear vpv; % CRASH also
unloadlibrary 'rriMonitor'
clear vpu; % everything fine
clear vpv; % CRASH
到目前为止,这是有效的。我的问题是决赛:
clear vpv (alias pstr in the example)
崩溃了matlab((R2011a)64位)
如何清除lib.pointer?
(我尝试更改java vm,但行为相同)
MATLAB Version : 7.12.0.635 (R2011a)
Operating System: Microsoft Windows 7
Processor ID : x86 Family 6 Model 14 Stepping 5, GenuineIntel
Virtual Machine : Java 1.7.0_09-b05 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 6.1 (Build 7601: Service Pack 1)
Fault Count: 1
Abnormal termination:
Segmentation violation
Register State (from fault):
RAX = 00000000015ccf68 RBX = 000000000e6faab0
RCX = 0000000001e90000 RDX = 0000000000000000
RSP = 00000000015ccef0 RBP = 0000000000000000
RSI = 0000000001e90000 RDI = 000000000e6faac0
R8 = 000000000e6faac0 R9 = 00000000040fa250
R10 = 0000000029c25fa0 R11 = 00000000015cd090
R12 = 000000000422e750 R13 = 0000000000000000
R14 = 0000000000000001 R15 = 000000006156fd40
RIP = 000000007709324e EFL = 00010246
CS = 0033 FS = 0053 GS = 002b
Stack Trace (from fault):
[ 0] 0x000000007709324e C:/Windows/SYSTEM32/ntdll.dll+00340558 ( RtlFreeHeap+000078 )
[ 1] 0x000000007698300a C:/Windows/system32/kernel32.dll+00143370 ( HeapFree+000010 )
[ 2] 0x000000006b89cabc C:/Windows/WinSxS/amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251/MSVCR90.dll+00314044 ( free+000028 )
[ 3] 0x000007fef131f9d9 C:/Program Files/MATLAB/R2011a/bin/win64/libmwcli.dll+00063961 ( RefDataType::AssignPointer+002153 )
[ 4] 0x000007fef131cb2d C:/Program Files/MATLAB/R2011a/bin/win64/libmwcli.dll+00052013 ( callstdfcn+005693 )
[ 5] 0x000007fef131c7a4 C:/Program Files/MATLAB/R2011a/bin/win64/libmwcli.dll+00051108 ( callstdfcn+004788 )
[ 6] 0x000000005f0a0d83 C:/Program Files/MATLAB/R2011a/bin/win64/udd.dll+00200067 ( UDInterface::destroyInternals+000067 )
[ 7] 0x000000005f0a0f06 C:/Program Files/MATLAB/R2011a/bin/win64/udd.dll+00200454 ( UDInterface::destroyUnconditional+000230 )
[ 8] 0x000000005f0a1034 C:/Program Files/MATLAB/R2011a/bin/win64/udd.dll+00200756 ( UDInterface::copyE+000052 )
[ 9] 0x000000005e1f0750 C:/Program Files/MATLAB/R2011a/bin/win64/udd_mi.dll+00264016 ( judiSetDynamicPropertyValue+008368 )
[ 10] 0x0000000075a28723 C:/Program Files/MATLAB/R2011a/bin/win64/libmx.dll+00034595 ( mxSetObjectCreateDestroyFn+000275 )
[ 11] 0x0000000075a288aa C:/Program Files/MATLAB/R2011a/bin/win64/libmx.dll+00034986 ( mxSetObjectCreateDestroyFn+000666 )
[ 12] 0x0000000075a7e8e5 C:/Program Files/MATLAB/R2011a/bin/win64/libmx.dll+00387301 ( mxClearArrayContents+000021 )
[ 13] 0x0000000051672928 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00731432 ( inDestroyWS+465608 )
[ 14] 0x0000000051666932 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00682290 ( inDestroyWS+416466 )
[ 15] 0x00000000516754ee C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00742638 ( inDestroyWS+476814 )
[ 16] 0x0000000051675930 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00743728 ( inDestroyWS+477904 )
[ 17] 0x00000000615201a9 C:/Program Files/MATLAB/R2011a/bin/win64/m_dispatcher.dll+00065961 ( mdLogging::log_function_entry+000201 )
[ 18] 0x0000000061520156 C:/Program Files/MATLAB/R2011a/bin/win64/m_dispatcher.dll+00065878 ( mdLogging::log_function_entry+000118 )
[ 19] 0x000000006151fb98 C:/Program Files/MATLAB/R2011a/bin/win64/m_dispatcher.dll+00064408 ( Mfunction_handle::dispatch+000456 )
[ 20] 0x000000005164182b C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00530475 ( inDestroyWS+264651 )
[ 21] 0x000000005163e082 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00516226 ( inDestroyWS+250402 )
[ 22] 0x0000000051654c73 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00609395 ( inDestroyWS+343571 )
[ 23] 0x0000000051654dc0 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00609728 ( inDestroyWS+343904 )
[ 24] 0x000000005164624b C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00549451 ( inDestroyWS+283627 )
[ 25] 0x00000000516442ac C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00541356 ( inDestroyWS+275532 )
[ 26] 0x0000000051651009 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00593929 ( inDestroyWS+328105 )
[ 27] 0x000000006151f859 C:/Program Files/MATLAB/R2011a/bin/win64/m_dispatcher.dll+00063577 ( Mfh_file::dispatch_fh+000473 )
[ 28] 0x000000006151fccf C:/Program Files/MATLAB/R2011a/bin/win64/m_dispatcher.dll+00064719 ( Mfunction_handle::dispatch+000767 )
[ 29] 0x00000000516c6de8 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01076712 ( inSetPredictor+001000 )
[ 30] 0x00000000516d3415 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01127445 ( inInitInterestRegistry+004309 )
[ 31] 0x00000000516d5175 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01134965 ( inIsCodeBlockComplete+007445 )
[ 32] 0x00000000516d5148 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01134920 ( inIsCodeBlockComplete+007400 )
[ 33] 0x00000000516d510e C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01134862 ( inIsCodeBlockComplete+007342 )
[ 34] 0x0000000051698113 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+00885011 ( inGetSeqVarPtrsFromCurrentWS+007315 )
[ 35] 0x00000000516c98e0 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01087712 ( inEvalCmdWithLocalReturn+000304 )
[ 36] 0x00000000516c983c C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01087548 ( inEvalCmdWithLocalReturn+000140 )
[ 37] 0x00000000516c97f1 C:/Program Files/MATLAB/R2011a/bin/win64/m_interpreter.dll+01087473 ( inEvalCmdWithLocalReturn+000065 )
[ 38] 0x0000000079fb833a C:/Program Files/MATLAB/R2011a/bin/win64/libmwbridge.dll+00033594 ( mnDebugPrompt+000106 )
[ 39] 0x0000000079fbd0d2 C:/Program Files/MATLAB/R2011a/bin/win64/libmwbridge.dll+00053458 ( mnParser+000418 )
[ 40] 0x00000000749ab604 C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00243204 ( mcrInstance::mnParser_on_interpreter_thread+000036 )
[ 41] 0x000000007498ed24 C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00126244 ( DepfunRules::postWarning+045636 )
[ 42] 0x000000007498edb1 C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00126385 ( DepfunRules::postWarning+045777 )
[ 43] 0x0000000001b79676 C:/Program Files/MATLAB/R2011a/bin/win64/uiw.dll+00366198 ( UIW_IsUserMessage+000086 )
[ 44] 0x0000000001b79d44 C:/Program Files/MATLAB/R2011a/bin/win64/uiw.dll+00367940 ( ws_ProcessPendingEventsWaitForWindows+000356 )
[ 45] 0x0000000076f687fe C:/Windows/system32/USER32.dll+00165886 ( GetMenuBarInfo+000638 )
[ 46] 0x0000000076f4f5fb C:/Windows/system32/USER32.dll+00062971 ( SystemParametersInfoW+000235 )
[ 47] 0x0000000076f54895 C:/Windows/system32/USER32.dll+00084117 ( IsProcessDPIAware+000465 )
[ 48] 0x0000000077091225 C:/Windows/SYSTEM32/ntdll.dll+00332325 ( KiUserCallbackDispatcher+000031 )
[ 49] 0x0000000076f59e6a C:/Windows/system32/USER32.dll+00106090 ( SfmDxSetSwapChainStats+000026 )
[ 50] 0x0000000076f5615e C:/Windows/system32/USER32.dll+00090462 ( GetMessageA+000078 )
[ 51] 0x0000000001b53f07 C:/Program Files/MATLAB/R2011a/bin/win64/uiw.dll+00212743 ( UIW_SetCurrentDialog+000855 )
[ 52] 0x0000000001b7b117 C:/Program Files/MATLAB/R2011a/bin/win64/uiw.dll+00373015 ( ws_FreeSystemFont+000519 )
[ 53] 0x000000005e36b7b3 C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00178099 ( sysq::ws_ppeHook::~ws_ppeHook+009539 )
[ 54] 0x000000005e36cf3c C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00184124 ( svWS_CommitDatabaseUpdates+002796 )
[ 55] 0x000000005e36d97a C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00186746 ( svWS_CommitDatabaseUpdates+005418 )
[ 56] 0x000000005e36dbbd C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00187325 ( svWS_CommitDatabaseUpdates+005997 )
[ 57] 0x000000005e36ddb8 C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00187832 ( svWS_CommitDatabaseUpdates+006504 )
[ 58] 0x000000005e36df10 C:/Program Files/MATLAB/R2011a/bin/win64/libuij.dll+00188176 ( svWS_CommitDatabaseUpdates+006848 )
[ 59] 0x000000007498e28f C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00123535 ( DepfunRules::postWarning+042927 )
[ 60] 0x000000007498f051 C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00127057 ( DepfunRules::postWarning+046449 )
[ 61] 0x000000007498f37e C:/Program Files/MATLAB/R2011a/bin/win64/mcr.dll+00127870 ( DepfunRules::postWarning+047262 )
[ 62] 0x000000013f527304 C:/Program Files/MATLAB/R2011a/bin/win64/MATLAB.exe+00029444 ( ???+000000 )
[ 63] 0x000000013f5273d9 C:/Program Files/MATLAB/R2011a/bin/win64/MATLAB.exe+00029657 ( ???+000000 )
[ 64] 0x000000013f5278cb C:/Program Files/MATLAB/R2011a/bin/win64/MATLAB.exe+00030923 ( ???+000000 )
[ 65] 0x000000007697652d C:/Windows/system32/kernel32.dll+00091437 ( BaseThreadInitThunk+000013 )
[ 66] 0x000000007706c521 C:/Windows/SYSTEM32/ntdll.dll+00181537 ( RtlUserThreadStart+000033 )
答案 0 :(得分:2)
SOLVE
动态分配的内存必须首先在c中释放!
更改c-dll功能
void test(char **u1, char **y1)
{
std::string recvString("Test Output");
*y1 = strdup(recvString.c_str()); //allocate and copy
}
添加c-dll功能
void clear(char** y1)
{
free(*y1)
*y1 = NULL;
}
添加matlab
loadlibrary('rriMonitor.dll', 'rriMonitor.h');
vpu = libpointer('stringPtrPtr',{'test input'});
vpv = libpointer('stringPtrPtr',{''});
calllib('rriMonitor','test',vpu,vpv);
vpv.value{1} %gives correct string 'Test Output'
calllib('rriMonitor','clear',vpv);
vpv.value %null pointer {[]}
clear all %works
unloadlibrary 'rriMonitor'
答案 1 :(得分:1)
strdup()
使用malloc()
创建新缓冲区。
因此,您需要分配两倍所需的内存并返回使用错误分配器分配的内存:
void test(char **u1, char **y1)
{
std::string recvString("Test Output");
// Allocates a MATLAB-compatible memory buffer for the new string.
*y1 = (char*) mxRealloc(*y1, recvString.size()+1);
// Creates another memory buffer for the new string using a memory
// allocator that's incompatible with MATLAB's automatic memory manager.
//
// Additionally, the pointer to the 1st memory buffer is lost and
// there is a memory leak.
*y1 = strdup(recvString.c_str());
}
当MATLAB尝试释放您返回的内存时发生崩溃。由于内存缓冲区与其内存管理器不兼容,因此会违反某些假设并导致崩溃。
你可能想写:
void test(char **u1, char **y1)
{
std::string recvString("Test Output");
// Allocate memory buffer using MATLAB-compatible memory.
*y1 = (char*) mxRealloc(*y1, recvString.size()+1);
// Copy the string contents into the MATLAB-compatible memory buffer.
strcpy(*y1, recvString.c_str());
}