![](/img/trans.png)
[英]How to use OpenMP in my MEX-file without crashing MATLAB?
[英]mex file is crashing, how to use MATLAB_MEM_MGR in matlab?
我已將 c++ 代碼編譯為 MEX 文件,但在調用此 MEX 文件時,它會崩潰。 它在 MATLAB 中給出以下錯誤消息:
檢測到分段違規
我嘗試在 C++ 文件中使用 try-catch 來打印 catch 塊中的消息,例如,
try {
//my code;
}
catch(std::exception &e)
{
mexPrintf(e.what());
mexEvalString("drawnow;");
return;
}
但打印消息不起作用,代碼仍然崩潰。
在查看 Google 時,大多數情況下它指向 MathWorks 給出的某種形式的消息: http://www.mathworks.de/matlabcentral/newsreader/view_thread/25900
它指示設置環境變量"MATLAB_MEM_MGR=debug"
,但沒有解釋如何使用它? 誰能解釋一下?
首先,try/catch 不會捕獲分段違規。 它只捕獲 C++ 異常,而不是像 sigsegv 這樣的信號。
其次,“使用” MATLAB_MEM_MGR
:
MATLAB_MEM_MGR
設置為“調試”(如 Windows 上的命令提示符或 Unix 上的終端),As Q3.5 of the FAQ says, if the MEX-function corrupts memory by (for example) writing past the end of a MATLAB-allocated block of memory, MATLAB will report the corruption when the block of memory is freed.
您可能想嘗試在調試器下運行 MEX 函數。 本技術說明有幾個鏈接,描述了如何在各種平台上執行此操作。
編輯:以前的鏈接已失效,這是最新的文檔頁面。
刪除mexEvalString("drawnow;")
。 我花了 5 個小時才弄清楚這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.