MATLAB: Why do i get the segmentation error while running a mex file? how do i solve this

segmentation error

Segmentation violation detected at Wed May 06 10:52:35 2015 ————————————————————————
Configuration: Crash Decoding : Disabled Default Encoding : windows-1252 MATLAB Architecture: win64 MATLAB Root : C:\Program Files\MATLAB\R2013a MATLAB Version : 8.1.0.604 (R2013a) Operating System : Microsoft Windows 7 Processor ID : x86 Family 6 Model 60 Stepping 3, GenuineIntel Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpotâ„¢ 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 = 000087b8544ba344 RBX = ffffffff98108450 RCX = ffffffff98108450 RDX = 000000009809b880 RSP = 0000000004026600 RBP = 0000000004027680 RSI = 00000000980adfa0 RDI = 0000000000000000
R8 = 0000000000000000 R9 = 0000000000000201
R10 = 0000000075a20000 R11 = 0000000004027680
R12 = 000000000000127f R13 = 000000002268bc30
R14 = 0000000000000003 R15 = 0000000004027680
RIP = 000007fef98219a5 EFL = 00010206
CS = 0033 FS = 0053 GS = 002b
Stack Trace (from fault): [ 0] 0x000007fef98219a5 D:\mangalore\downloads\SFMedu2\denseMatch\priority_queue_1.0\pq_push.mexw64+00006565 [ 1] 0x0000000004e2f731 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00063281 mexRunMexFile+00000129 [ 2] 0x0000000004e2e202 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00057858 inSwapMexfileReader+00000226 [ 3] 0x0000000004e2f2b4 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00062132 _constructor_lm_mexmaci64+00001620 [ 4] 0x0000000074e8c64f C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00050767 Mfh_file::dispatch_fh+00000623 [ 5] 0x0000000074e8ced2 C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00052946 Mfunction_handle::dispatch+00000802 [ 6] 0x00000000046dbc7b C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00834683 inDestroyWS+00392123 [ 7] 0x00000000046cb091 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00766097 inDestroyWS+00323537 [ 8] 0x00000000046b008f C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00655503 inDestroyWS+00212943 [ 9] 0x00000000046c65a4 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00746916 inDestroyWS+00304356 [ 10] 0x00000000046bc3b0 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00705456 inDestroyWS+00262896 [ 11] 0x00000000046b8fe4 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00692196 inDestroyWS+00249636 [ 12] 0x00000000046d021f C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00786975 inDestroyWS+00344415 [ 13] 0x00000000046d0370 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00787312 inDestroyWS+00344752 [ 14] 0x00000000046af1dd C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00651741 inDestroyWS+00209181 [ 15] 0x00000000046b0bd0 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00658384 inDestroyWS+00215824 [ 16] 0x000000000469cf79 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00577401 inDestroyWS+00134841 [ 17] 0x0000000074e8c64f C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00050767 Mfh_file::dispatch_fh+00000623 [ 18] 0x0000000074e8ced2 C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00052946 Mfunction_handle::dispatch+00000802 [ 19] 0x00000000046b6d48 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00683336 inDestroyWS+00240776 [ 20] 0x000000000462f9f7 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00129527 inFullEvalFcn+00022023 [ 21] 0x00000000046ba877 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00698487 inDestroyWS+00255927 [ 22] 0x00000000046d021f C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00786975 inDestroyWS+00344415 [ 23] 0x00000000046d0370 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00787312 inDestroyWS+00344752 [ 24] 0x00000000046af1dd C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00651741 inDestroyWS+00209181 [ 25] 0x00000000046b0bd0 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00658384 inDestroyWS+00215824 [ 26] 0x000000000469cf79 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00577401 inDestroyWS+00134841 [ 27] 0x0000000074e8c64f C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00050767 Mfh_file::dispatch_fh+00000623 [ 28] 0x0000000074e8ced2 C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00052946 Mfunction_handle::dispatch+00000802 [ 29] 0x000000000461e70e C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00059150 inValidateLoadedObject+00002350 [ 30] 0x0000000004726495 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01139861 IInterpreter::IInterpreter+00000229 [ 31] 0x000000000472ca45 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165893 inRegisterFunctionHandles+00004405 [ 32] 0x000000000472ca12 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165842 inRegisterFunctionHandles+00004354 [ 33] 0x000000000472c9d7 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165783 inRegisterFunctionHandles+00004295 [ 34] 0x000000000471ba0e C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01096206 inGetSeqVarPtrsFromCurrentWS+00005742 [ 35] 0x000000000461be89 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048777 inEvalCmdWithLocalReturn+00000329 [ 36] 0x000000000461bdcc C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048588 inEvalCmdWithLocalReturn+00000140 [ 37] 0x000000000461bd81 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048513 inEvalCmdWithLocalReturn+00000065 [ 38] 0x0000000079fbbecd C:\Program Files\MATLAB\R2013a\bin\win64\libmwbridge.dll+00048845 mnDebugPrompt+00000109 [ 39] 0x0000000079fbd01b C:\Program Files\MATLAB\R2013a\bin\win64\libmwbridge.dll+00053275 mnParser+00000763 [ 40] 0x000000007499daa4 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00187044 mcrInstance::mnParser_on_interpreter_thread+00000036 [ 41] 0x0000000074982220 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00074272 mcr::setInterpreterThreadToCurrent+00031664 [ 42] 0x0000000074982280 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00074368 mcr::setInterpreterThreadToCurrent+00031760 [ 43] 0x0000000005ebf9d6 C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00391638 UIW_IsUserMessage+00000086 [ 44] 0x0000000005ec0095 C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00393365 UIW_SetGLIMUserMsg+00000117 [ 45] 0x00000000773187fe C:\Windows\system32\USER32.dll+00165886 GetMenuBarInfo+00000638 [ 46] 0x00000000772ff5fb C:\Windows\system32\USER32.dll+00062971 SystemParametersInfoW+00000235 [ 47] 0x0000000077304895 C:\Windows\system32\USER32.dll+00084117 IsProcessDPIAware+00000465 [ 48] 0x00000000774411f5 C:\Windows\SYSTEM32\ntdll.dll+00332277 KiUserCallbackDispatcher+00000031 [ 49] 0x000000007730908a C:\Windows\system32\USER32.dll+00102538 PeekMessageW+00000186 [ 50] 0x00000000773050fe C:\Windows\system32\USER32.dll+00086270 GetKeyState+00000238 [ 51] 0x0000000077303a6f C:\Windows\system32\USER32.dll+00080495 PeekMessageA+00000087 [ 52] 0x0000000005e99a1b C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00236059 UIW_SetCurrentDialog+00000731 [ 53] 0x0000000005ec1532 C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00398642 ws_FreeSystemFont+00000482 [ 54] 0x0000000004338930 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00887088 services::system_events::PpeDispatchHook::~PpeDispatchHook+00055600 [ 55] 0x00000000043392f5 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00889589 services::system_events::PpeDispatchHook::~PpeDispatchHook+00058101 [ 56] 0x000000000433a100 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00893184 services::system_events::PpeDispatchHook::~PpeDispatchHook+00061696 [ 57] 0x00000000043428d2 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00927954 svWS_ProcessPendingEvents+00001090 [ 58] 0x000000007498329d C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00078493 mcr::setInterpreterThreadToCurrent+00035885 [ 59] 0x0000000074983562 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00079202 mcr::setInterpreterThreadToCurrent+00036594 [ 60] 0x0000000074976915 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00026901 mcrInstantiationError::operator=+00003781 [ 61] 0x0000000074979d52 C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00040274 mcr_invoke_on_interpreter_thread+00001602 [ 62] 0x0000000140005591 C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00021905 [ 63] 0x0000000140005c3a C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00023610 [ 64] 0x0000000140006103 C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00024835 [ 65] 0x00000000771e59ed C:\Windows\system32\kernel32.dll+00088557 BaseThreadInitThunk+00000013 [ 66] 0x000000007741c541 C:\Windows\SYSTEM32\ntdll.dll+00181569 RtlUserThreadStart+00000033
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 C:\Users\smv02\AppData\Local\Temp\matlab_crash_dump.4016-1
Caught MathWorks::System::FatalException [Please exit and restart MATLAB]>>
this is the error i get while running a simple mex file to push values to stack

Best Answer

What are the types of the variables you are passing in? E.g., here is a check at the beginning or mexFunction for the 1st argument:
if( !mxIsNumeric(prhs[0]) )
mexErrMsgTxt("parameter 1 missing!\n");
So 1st argument will get past this check if it is any numeric class (e.g., int32). Then you pass it here:
retrieve_heap( prhs[0], heap);
And you use it here:
void retrieve_heap( const mxArray* matptr, MaxHeap<double>* & heap){
// retrieve pointer from the MX form
double* pointer0 = mxGetPr(matptr);
// check that I actually received something
if( pointer0 == NULL )
mexErrMsgTxt("vararg{1} must be a valid priority queue pointer\n");
// convert it to "long" datatype (good for addresses)
long pointer1 = (long) pointer0[0];
// convert it to "KDTree"
heap = (MaxHeap<double>*) pointer1;
But this latter code will only work if the 1st argument is double class. It will not work for any other class, and will likely seg fault if it is a shorter class (e.g., int32).
Also, you are type punning here ... taking a double and converting it to a long and then type casting it to a pointer. That will only have a chance of working on a 32-bit system where pointers will fit entirely in the mantissa of a double ... it will have no hope of working on a 64-bit system (to do that would necessitate using a uint64 or int64).
Where is this heap address originally coming from?
Related Question