Decision Optimization

  • 1.  Access violation exception using CP optimizer in MEX functions

    Posted Fri October 18, 2019 01:08 AM

    Originally posted by: Sulivan


    I have developed two C++ programmes using CP optimizer to solve scheduling problem, which are compiled to MEX functions to be called by Matlab. Both programmes work fine when they are running independently in different Matlab sessions. However, when I run on any one programme after another in the same Matlab session, the following exception is thrown:

    Exception thrown at (some position in memory) (main.mexw64) in MATLAB.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred

    Then the program crashes.

    I tried to debug the program, finding out that the except is thrown during the call of 

    IloCP cp(model)

    in the second MEX function.

    If I open two Matlab processes and run these MEX functions independently, then they would just work fine.

    Can anybody work around the reason? Thanks in advance.



  • 2.  Re: Access violation exception using CP optimizer in MEX functions

    Posted Fri October 18, 2019 04:24 AM

    Originally posted by: Philippe_Refalo


    It's difficult to determine where this could come from. Maybe the IloCP is created on a different IloEnv than the model or the IloEnv that is used to create it has been ended before (with env.end()). Could you check this ? 

    Thanks

    Philippe



  • 3.  Re: Access violation exception using CP optimizer in MEX functions

    Posted Sat October 19, 2019 12:32 AM

    Originally posted by: Sulivan


    Thank you for your reply. I checked what you mentioned. Both programmes are written using the following template:

    IloEnv env;
    IloModel model(env);
    /* Add constraints and objective */
    //Create an instance of IloCP
    IloCP cp(model);
    //IloCP cp(env);
    // cp.extract(model);
    // cp.setParameter(...);
    if (cp.solve()) {
    // Do something
    }
    //cp.end();
    env.end();
    

    I tried commenting and uncommenting cp.end() and env.end() in the last two lines. I have also tried using IloCP cp(env) and cp.extract(model). None of these worked, stilling receiving the same exception. The symptom is:

    1. Running these two programmes in different Matlab sessions, fine.

    2. Running any of them in the same Matlab session for multiple times, fine.

    3. Running one of them after the other in the same Matlab session, exception during the call of the constructor IloCP in the second programme.



  • 4.  Re: Access violation exception using CP optimizer in MEX functions

    Posted Mon October 21, 2019 05:16 AM

    Originally posted by: Petr Vilím


    Hello,

    let's try to catch the exception, maybe it contains some useful information. Otherwise it is hard to tell what's going on. Could you put the code into try-catch block like this?

    try {
        ...
    } catch (IloException& ex) {
        std::cerr << "Exception caught: " << ex << std::endl;
    }
    

    I assume you can see the error output somewhere in the Matlab (or use some other stream to print the exception).

    Thanks, Petr



  • 5.  Re: Access violation exception using CP optimizer in MEX functions

    Posted Mon October 21, 2019 06:37 AM

    Originally posted by: Sulivan


    Thank you for your suggestion. I tried IloException and std::exception both, nothing is shown but the still a crash of Matlab, showing the following report, which I don't think as useful. The only message I can get is Access Violation. 

    BTW, even if I remove all things (constraints, objective and everything else) from the model, just simply create an IloEnv, IloModel and IloCP, the exception still remains under the condition I mentioned.

    MATLAB crash file:


    --------------------------------------------------------------------------------
             Access violation detected at Monday Oct 21 06:27:26 2019 +0800
    --------------------------------------------------------------------------------

    Configuration:
      Crash Decoding           : Disabled - No sandbox or build area path
      Crash Mode               : continue (default)
      Default Encoding         : GBK
      Deployed                 : false
      Graphics Driver          : Unknown hardware 
      Graphics card 1          : xxxxxxxxx
      Graphics card 2          : xxxxxxxxx
      Java Version             : Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
      MATLAB Architecture      : win64
      MATLAB Entitlement ID    : 6257193
      MATLAB Root              : C:\Program Files\Polyspace\R2019b
      MATLAB Version           : 9.7.0.1190202 (R2019b
      OpenGL                   : hardware
      Operating System         : Microsoft Windows 7 Ultimate 
      Process ID               : 9524
      Processor ID             : x86 Family 6 Model 63 Stepping 2, GenuineIntel
      Session Key              : xxxxxxxxx
      Window System            : Version 6.1 (Build 7601: Service Pack 1)

    Fault Count: 1


    Abnormal termination:
    Access violation

    Register State (from fault):
      RAX = bf3c8451ab771fb6  RBX = 000007febd7dbe60
      RCX = 0000000000000001  RDX = 0000000004028ec8
      RSP = 0000000004028a70  RBP = 000000000402ae50
      RSI = 0000000000000008  RDI = 0000000000000000
     
       R8 = 0000000000000010   R9 = 0000000004028e60
      R10 = 0000000053093ff6  R11 = 00000000860899d5
      R12 = 0000000000000000  R13 = 000000000402bcd0
      R14 = ffffffffffffffff  R15 = 000000016f0e2ad0
     
      RIP = 000007febcda1d06  EFL = 00010202
     
       CS = 0033   FS = 0053   GS = 002b

    Stack Trace (from fault):
    [  0] 0x000007febcda1d06 F:\xxxxxxxxx\main_CP.mexw64+00072966 mexfilerequiredapiversion+00056502
    [  1] 0x000007febce28965 F:\xxxxxxxxx\main_CP.mexw64+00624997 mexfilerequiredapiversion+00608533
    [  2] 0x000007febce29744 F:\xxxxxxxxx\main_CP.mexw64+00628548 mexfilerequiredapiversion+00612084
    [  3] 0x000007febce0d23c F:\xxxxxxxxx\main_CP.mexw64+00512572 mexfilerequiredapiversion+00496108
    [  4] 0x000007febcd94e93 F:\xxxxxxxxx\main_CP.mexw64+00020115 mexfilerequiredapiversion+00003651
    [  5] 0x000007febcd97f59 F:\xxxxxxxxx\main_CP.mexw64+00032601 mexfilerequiredapiversion+00016137
    [  6] 0x000007febcd93098 F:\xxxxxxxxx\main_CP.mexw64+00012440 mexFunction+00000904
    [  7] 0x00000000fc636d86 C:\Program Files\Polyspace\R2019b\bin\win64\libmex.dll+00355718 mexfile::Inspector::needs_upgrade+00012918
    [  8] 0x00000000fc637f4b C:\Program Files\Polyspace\R2019b\bin\win64\libmex.dll+00360267 MexRetrieveVersion+00003339
    [  9] 0x00000000fc6380bc C:\Program Files\Polyspace\R2019b\bin\win64\libmex.dll+00360636 MexRetrieveVersion+00003708
    [ 10] 0x00000000fc638224 C:\Program Files\Polyspace\R2019b\bin\win64\libmex.dll+00360996 MexRetrieveVersion+00004068
    [ 11] 0x00000000fc61a2c9 C:\Program Files\Polyspace\R2019b\bin\win64\libmex.dll+00238281 mexUnlock_800+00025209
    [ 12] 0x000000000e255ef9                     bin\win64\pgo\m_dispatcher.dll+00614137 Mfh_file::dispatch_file_common+00000165
    [ 13] 0x000000000e255579                     bin\win64\pgo\m_dispatcher.dll+00611705 Mfh_error::lookup+00006517
    [ 14] 0x000000000e255e4d                     bin\win64\pgo\m_dispatcher.dll+00613965 Mfh_file::dispatch+00000045
    [ 15] 0x000000000f6dc733                            bin\win64\pgo\m_lxe.dll+00247603
    [ 16] 0x000000000f7fa775                            bin\win64\pgo\m_lxe.dll+01419125 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00436473
    [ 17] 0x000000000f6ce2ad                            bin\win64\pgo\m_lxe.dll+00189101
    [ 18] 0x000000000f6d052a                            bin\win64\pgo\m_lxe.dll+00197930
    [ 19] 0x000000000f6d141d                            bin\win64\pgo\m_lxe.dll+00201757
    [ 20] 0x000000000f6d100f                            bin\win64\pgo\m_lxe.dll+00200719
    [ 21] 0x000000000f82019c                            bin\win64\pgo\m_lxe.dll+01573276 MathWorks::lxe::StartupLxeEngine+00033000
    [ 22] 0x000000000f6d9ae2                            bin\win64\pgo\m_lxe.dll+00236258
    [ 23] 0x000000000f6cf8b5                            bin\win64\pgo\m_lxe.dll+00194741
    [ 24] 0x000000000f6ce9cc                            bin\win64\pgo\m_lxe.dll+00190924
    [ 25] 0x000000000f7c3087                            bin\win64\pgo\m_lxe.dll+01192071 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00209419
    [ 26] 0x000000000f7c2f67                            bin\win64\pgo\m_lxe.dll+01191783 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00209131
    [ 27] 0x000000000f7c2e5e                            bin\win64\pgo\m_lxe.dll+01191518 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00208866
    [ 28] 0x000000000f7c38ac                            bin\win64\pgo\m_lxe.dll+01194156 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00211504
    [ 29] 0x000000000f7c385a                            bin\win64\pgo\m_lxe.dll+01194074 mwboost::archive::detail::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>::oserializer<mwboost::archive::binary_oarchive,foundation::msg_svc::eventmgr::BaseEvent>+00211422
    [ 30] 0x00000000111be28c                            bin\win64\pgo\m_lxe.dll+28435084 MathWorks::lxe::profiler::IMexProfilingSupport::~IMexProfilingSupport+00083224
    [ 31] 0x000000000f48b19c C:\Program Files\Polyspace\R2019b\bin\win64\libmwbridge.dll+00176540 mnParser+00001212
    [ 32] 0x000000000f30d5f7 C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00906743 mcrInstance::releaseThread+00002647
    [ 33] 0x000000000f24a0ca C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00106698 mcrOptions::set_use_license_manager+00068938
    [ 34] 0x000000000f26a334 C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00238388 mcrOptions::set_use_license_manager+00200628
    [ 35] 0x00000000fd02047e C:\Program Files\Polyspace\R2019b\bin\win64\iqm.dll+00590974 iqm::PackagedTaskPlugin::execute+00000878
    [ 36] 0x00000000fd020303 C:\Program Files\Polyspace\R2019b\bin\win64\iqm.dll+00590595 iqm::PackagedTaskPlugin::execute+00000499
    [ 37] 0x000000000f2b37de C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00538590 mcr::runtime::setInterpreterThreadSingletonToCurrent+00188382
    [ 38] 0x00000000fcffca21 C:\Program Files\Polyspace\R2019b\bin\win64\iqm.dll+00444961 iqm::Iqm::setupIqmFcnPtrs+00101969
    [ 39] 0x00000000fcfd3ab7 C:\Program Files\Polyspace\R2019b\bin\win64\iqm.dll+00277175 iqm::Iqm::create+00009159
    [ 40] 0x00000000fcfd326d C:\Program Files\Polyspace\R2019b\bin\win64\iqm.dll+00275053 iqm::Iqm::create+00007037
    [ 41] 0x000000000f29120f C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00397839 mcr::runtime::setInterpreterThreadSingletonToCurrent+00047631
    [ 42] 0x000000000f2917fe C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00399358 mcr::runtime::setInterpreterThreadSingletonToCurrent+00049150
    [ 43] 0x000000000f29040c C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+00394252 mcr::runtime::setInterpreterThreadSingletonToCurrent+00044044
    [ 44] 0x000000000f3527d6 C:\Program Files\Polyspace\R2019b\bin\win64\mcr.dll+01189846 mcr::mvm::detail::activateMcr+00016598
    [ 45] 0x000000000e100dbf C:\Program Files\Polyspace\R2019b\bin\win64\MVMLocal.dll+00396735 mvm_server::inproc::LocalFactory::terminate+00187135
    [ 46] 0x00000000fa9e7c10 C:\Program Files\Polyspace\R2019b\bin\win64\mvm.dll+01801232 mvm::detail::SessionImpl::initWithOptions+00000736
    [ 47] 0x00000000fa9e8941 C:\Program Files\Polyspace\R2019b\bin\win64\mvm.dll+01804609 mvm::detail::SessionImpl::runMain+00000081
    [ 48] 0x00000000fa9e8bc7 C:\Program Files\Polyspace\R2019b\bin\win64\mvm.dll+01805255 mvm::detail::SessionImpl::runMatlabDesktop+00000279
    [ 49] 0x0000000140007824 C:\Program Files\Polyspace\R2019b\bin\win64\MATLAB.exe+00030756 mwboost::serialization::singleton_module::unlock+00001172
    [ 50] 0x00000001400086f2 C:\Program Files\Polyspace\R2019b\bin\win64\MATLAB.exe+00034546 mwboost::serialization::singleton_module::unlock+00004962
    [ 51] 0x00000000776e556d                   C:\windows\system32\kernel32.dll+00087405 BaseThreadInitThunk+00000013
    [ 52] 0x000000007794385d                      C:\windows\SYSTEM32\ntdll.dll+00342109 RtlUserThreadStart+00000029

     


  • 6.  Re: Access violation exception using CP optimizer in MEX functions

    Posted Mon October 21, 2019 07:15 AM

    Originally posted by: Petr Vilím


    This is very weird. Maybe we can simplify the issue even more. Could you try to get rid of IloModel and create just IloCP using the environment itself?

    IloEnv env;
    IloCP cp(env);
    

    This seems to be some memory issue, and it doesn't have to be just in IloCP (although, of course, it could be). Once the memory is corrupted the crash may occur in any code that allocates or deallocates memory (and constructor of IloCP does that). In particular, I found the following discussion:

    https://www.mathworks.com/matlabcentral/answers/25822-mex-file-crash-after-second-run

    So could you also have a look at the rest of the MEX function code? And maybe get rid of as much of this code as possible in order to create a minimal example..

    Thanks, Petr