title: Access violation error in matlab
body: <p>I am trying to run the following matlab code in matlab 2016: <a href="http://pastebin.com/q42xhMfw" rel="nofollow">http://pastebin.com/q42xhMfw</a> which uses the following C++ compiled code (compiled using mingw64 compiler): <a href="https://github.com/rajatsaxena/IISc_Neuroscience/blob/master/Miscellaneous/mexAdaptwhile.c" rel="nofollow">https://github.com/rajatsaxena/IISc_Neuroscience/blob/master/Miscellaneous/mexAdaptwhile.c</a>. Compiling the C file gives me mexw64 file but when I run it, I get an Access violation error which I think is because of some issue related to compilation or address allocation cause this script works sometimes and not other times. Here is the error log:</p>
  
  <pre><code>------------------------------------------------------------------------
            Access violation detected at Thu Oct 27 19:40:42 2016
  ------------------------------------------------------------------------
  
  Configuration:
    Crash Decoding      : Disabled
    Crash Mode          : continue (default)
    Current Graphics Driver: Unknown hardware 
    Default Encoding    : windows-1252
    Graphics card 1     : Intel Corporation ( 0x8086 ) Intel(R) HD Graphics 4600 Version 20.19.15.4444
    Host Name           : user
    MATLAB Architecture : win64
    MATLAB Root         : C:\Program Files\MATLAB\R2016a
    MATLAB Version      : 9.0.0.341360 (R2016a)
    OpenGL              : hardware
    Operating System    : Microsoft Windows 10 Home Single Language
    Processor ID        : x86 Family 6 Model 60 Stepping 3, GenuineIntel
    Virtual Machine     : Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
    Window System       : Version 10.0 (Build 10586)
  
  Fault Count: 1
  
  
  Abnormal termination:
  Access violation
  
  Register State (from fault):
    RAX = 0000000037fa41e8  RBX = 0000000037de0300
    RCX = 0000000000089e20  RDX = fffffffffffbf1c0
    RSP = 00000000045f9d30  RBP = 0000000037f9fe60
    RSI = 0000000000000022  RDI = 0000000000000038
  
     R8 = 0000000000000006   R9 = 0000000000000038
    R10 = 0000000000000850  R11 = 0000000037fa40e8
    R12 = 00000000000001c0  R13 = 0000000037f5f020
    R14 = 0000000038029c80  R15 = 0000000000000048
  
    RIP = 00007ffbb95d1140  EFL = 00010206
  
     CS = 0033   FS = 0053   GS = 002b
  
  Stack Trace (from fault):
  [  0] 0x00007ffbb95d1140 C:\Users\user1\Desktop\Sachin_report\rate_maps\mexAdaptwhile.mexw64+00004416
  [  1] 0x00007ffbb95d13fe C:\Users\user1\Desktop\Sachin_report\rate_maps\mexAdaptwhile.mexw64+00005118 mexFunction+00000286
  [  2] 0x00000000fc5f77d1 C:\Program Files\MATLAB\R2016a\bin\win64\libmex.dll+00096209 mexRunMexFile+00000129
  [  3] 0x00000000fc5f6d12 C:\Program Files\MATLAB\R2016a\bin\win64\libmex.dll+00093458 inSwapMexfileReader+00000434
  [  4] 0x00000000fc5f6966 C:\Program Files\MATLAB\R2016a\bin\win64\libmex.dll+00092518 mexUnlock+00004774
  [  5] 0x0000000015bb5d1f C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00089375 Mfh_file::dispatch_fh_impl+00000527
  [  6] 0x0000000015bb5f0e C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00089870 Mfh_file::dispatch_fh+00000062
  [  7] 0x0000000015bb3e15 C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00081429 Mfunction_handle::dispatch+00000933
  [  8] 0x000000001675f451 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00848977 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00049101
  [  9] 0x000000001676428f C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00869007 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00069131
  [ 10] 0x0000000016764ef6 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00872182 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00072306
  [ 11] 0x0000000016766237 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00877111 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00077235
  [ 12] 0x0000000016766c05 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00879621 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00079745
  [ 13] 0x000000001676642f C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00877615 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00077739
  [ 14] 0x000000001676613a C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00876858 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00076982
  [ 15] 0x000000001675de5b C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00843355 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00043479
  [ 16] 0x000000001675bd27 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00834855 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00034979
  [ 17] 0x000000001675b1c2 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00831938 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00032062
  [ 18] 0x00000000166aee82 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00126594
  [ 19] 0x00000000166af107 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00127239
  [ 20] 0x0000000015bb5e95 C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00089749 Mfh_file::dispatch_fh_impl+00000901
  [ 21] 0x0000000015bb5f0e C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00089870 Mfh_file::dispatch_fh+00000062
  [ 22] 0x0000000015bb3e15 C:\Program Files\MATLAB\R2016a\bin\win64\m_dispatcher.dll+00081429 Mfunction_handle::dispatch+00000933
  [ 23] 0x00000000166e2033 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00335923
  [ 24] 0x0000000016888114 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+02064660 boost::serialization::singleton&lt;boost::archive::detail::pointer_oserializer&lt;boost::archive::binaryTerm_oarchive,ir::IrTree&gt; &gt;::get_instance+00682852
  [ 25] 0x000000001676428f C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00869007 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00069131
  [ 26] 0x0000000016764ef6 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00872182 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00072306
  [ 27] 0x0000000016766237 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00877111 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00077235
  [ 28] 0x0000000016766c05 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00879621 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00079745
  [ 29] 0x000000001676642f C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00877615 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00077739
  [ 30] 0x000000001676613a C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00876858 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00076982
  [ 31] 0x000000001675de5b C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00843355 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00043479
  [ 32] 0x000000001675bd27 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00834855 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00034979
  [ 33] 0x000000001675b1c2 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00831938 boost::serialization::singleton&lt;boost::archive::detail::pointer_iserializer&lt;boost::archive::binaryTerm_iarchive,ir::IrTree&gt; &gt;::get_instance+00032062
  [ 34] 0x00000000166e9d47 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00367943
  [ 35] 0x00000000166e9681 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00366209
  [ 36] 0x00000000166e9810 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00366608
  [ 37] 0x000000001689d395 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+02151317 boost::serialization::singleton&lt;boost::archive::detail::pointer_oserializer&lt;boost::archive::binaryTerm_oarchive,ir::IrTree&gt; &gt;::get_instance+00769509
  [ 38] 0x000000001689d31e C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+02151198 boost::serialization::singleton&lt;boost::archive::detail::pointer_oserializer&lt;boost::archive::binaryTerm_oarchive,ir::IrTree&gt; &gt;::get_instance+00769390
  [ 39] 0x00000000166bdad1 C:\Program Files\MATLAB\R2016a\bin\win64\m_lxe.dll+00187089
  [ 40] 0x0000000015d40241 C:\Program Files\MATLAB\R2016a\bin\win64\m_interpreter.dll+00787009 inEvalCmdWithLocalReturn+00000065
  [ 41] 0x00000000fb6056cd C:\Program Files\MATLAB\R2016a\bin\win64\libmwbridge.dll+00087757 mnGetPrompt+00005933
  [ 42] 0x00000000fb606473 C:\Program Files\MATLAB\R2016a\bin\win64\libmwbridge.dll+00091251 mnParser+00001091
  [ 43] 0x0000000015ab99d1   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00235985 mcr::runtime::setInterpreterThreadToCurrent+00025585
  [ 44] 0x0000000015ab8967   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00231783 mcr::runtime::setInterpreterThreadToCurrent+00021383
  [ 45] 0x0000000015ab89e3   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00231907 mcr::runtime::setInterpreterThreadToCurrent+00021507
  [ 46] 0x0000000015ab92d1   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00234193 mcr::runtime::setInterpreterThreadToCurrent+00023793
  [ 47] 0x00000000fd01ea57   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00584279 iqm::UserEvalPlugin::pre+00030535
  [ 48] 0x00000000fd02b3ac   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00635820 iqm::UserEvalPlugin::pre+00082076
  [ 49] 0x00000000fd018bf0   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00560112 iqm::UserEvalPlugin::pre+00006368
  [ 50] 0x00000000fd02ddaa   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00646570 iqm::UserEvalPlugin::pre+00092826
  [ 51] 0x00000000fcffbd07   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00441607 iqm::PackagedTaskPlugin::PackagedTaskPlugin+00000727
  [ 52] 0x00000000fcffc35f   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00443231 iqm::PackagedTaskPlugin::execute+00000575
  [ 53] 0x00000000fcffbd79   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00441721 iqm::PackagedTaskPlugin::PackagedTaskPlugin+00000841
  [ 54] 0x00000000fcffc1d4   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00442836 iqm::PackagedTaskPlugin::execute+00000180
  [ 55] 0x0000000015acb4a5   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00308389 mcrRegisterExternalFunction+00024629
  [ 56] 0x00000000fcfd62ea   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00287466 iqm::Iqm::setupIqmFcnPtrs+00069674
  [ 57] 0x00000000fcfd61d7   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00287191 iqm::Iqm::setupIqmFcnPtrs+00069399
  [ 58] 0x00000000fcfbbae1   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00178913 iqm::Iqm::deliver+00003409
  [ 59] 0x00000000fcfbc9df   C:\Program Files\MATLAB\R2016a\bin\win64\iqm.dll+00182751 iqm::Iqm::deliver+00007247
  [ 60] 0x00000001001a14d3 C:\Program Files\MATLAB\R2016a\bin\win64\libmwservices.dll+01774803 services::system_events::PpeDispatchHook::dispatchOne+00031459
  [ 61] 0x00000001001ae933 C:\Program Files\MATLAB\R2016a\bin\win64\libmwservices.dll+01829171 sysq::addProcessPendingEventsUnitTestHook+00005491
  [ 62] 0x00000001001aeb10 C:\Program Files\MATLAB\R2016a\bin\win64\libmwservices.dll+01829648 sysq::addProcessPendingEventsUnitTestHook+00005968
  [ 63] 0x00000001001b0665 C:\Program Files\MATLAB\R2016a\bin\win64\libmwservices.dll+01836645 sysq::getCondition+00003813
  [ 64] 0x00000001001b2422 C:\Program Files\MATLAB\R2016a\bin\win64\libmwservices.dll+01844258 svWS_ProcessPendingEvents+00000162
  [ 65] 0x0000000015ab9d0d   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00236813 mcr::runtime::setInterpreterThreadToCurrent+00026413
  [ 66] 0x0000000015aba3fa   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00238586 mcr::runtime::setInterpreterThreadToCurrent+00028186
  [ 67] 0x0000000015ab1a05   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00203269 mcr_process_events+00008629
  [ 68] 0x0000000015ab3462   C:\Program Files\MATLAB\R2016a\bin\win64\mcr.dll+00210018 mcr_process_events+00015378
  [ 69] 0x00000000159293be C:\Program Files\MATLAB\R2016a\bin\win64\MVMLocal.dll+00234430 mvm_server::inproc::LocalFactory::terminate+00071182
  [ 70] 0x00000000fa93cd89   C:\Program Files\MATLAB\R2016a\bin\win64\mvm.dll+01101193 mvm::detail::initLocalMvmHack+00000521
  [ 71] 0x00000000fa93d355   C:\Program Files\MATLAB\R2016a\bin\win64\mvm.dll+01102677 mvm::detail::SessionImpl::privateSession+00000373
  [ 72] 0x00000000fa93d561   C:\Program Files\MATLAB\R2016a\bin\win64\mvm.dll+01103201 mvm::detail::SessionImpl::privateSession+00000897
  [ 73] 0x0000000140006fd5 C:\Program Files\MATLAB\R2016a\bin\win64\MATLAB.exe+00028629
  [ 74] 0x0000000140007661 C:\Program Files\MATLAB\R2016a\bin\win64\MATLAB.exe+00030305
  [ 75] 0x00007ffbc6c98102                   C:\WINDOWS\system32\KERNEL32.DLL+00098562 BaseThreadInitThunk+00000034
  [ 76] 0x00007ffbc71cc5b4                      C:\WINDOWS\SYSTEM32\ntdll.dll+00378292 RtlUserThreadStart+00000052
  
  
  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\user1\AppData\Local\Temp\matlab_crash_dump.32-1 **
  
  
  Caught MathWorks::System::FatalException
  [Please exit and restart MATLAB]&gt;&gt; 
  </code></pre>
  
  <p>How do I sort this out, or what changes need to be made? Thanks!</p>