5

I am in the process of developing an SFunction and compiled submodel in Simulink, an SFunction that calls into a DLL compiled using Realtime Workshop. The problem is, once I compile the submodel once, execute it in native Simulink through the SFunction and the model finishes, MATLAB still keeps a handle to the submodel DLL. I cannot recompile the DLL until MATLAB is restarted, forcing the release of the DLL.

I have even gone so far as to see if ProcessExplorer could force closed the handle but it can't.

J Collins
  • 2,106
  • 1
  • 23
  • 30
  • Have you tried closing the model? Does it make a difference? Also, have you tried `clear functions` after closing the model? It should clear all MEX functions from memory. – am304 Dec 18 '13 at 16:05
  • 3
    Have tried: (1) clear , (2) clear mex, (3) clear all, (4) bdclose all, (5) unloadlibrary(), (6) closing the model, (7) closing all models; (8) using SysInternals ProcessExplorer to close the handle; (9) using Unlocker to unlock/delete the DLL and (10) calling FreeLibrary() at the end of the SFunction. None of these seem to influence the problem. – J Collins Dec 18 '13 at 16:17
  • :-( More than I would have tried... Sorry, don't have any other idea. – am304 Dec 18 '13 at 16:27
  • What does `islibloaded('LIBNAME')` return? – Florian Brucker Dec 18 '13 at 17:54
  • 1
    How do you load/call into your dll? Do you unload it after you are done in your mdlTerminate function? – Navan Dec 18 '13 at 18:51
  • @Florian, it returns zero. – J Collins Dec 19 '13 at 09:25
  • @Navan I call FreeLibrary() on the DLL. The first time I tried it it caused a segmentation violation though am having limited success with it now. – J Collins Dec 19 '13 at 09:26
  • Depending on your settings mdlTerminate sometimes may get called without the full propagation. Make sure you check for valid value before calling FreeLibrary – Navan Dec 19 '13 at 18:41

2 Answers2

2

I think it would work if you just rename the actual DLL to something else and then recompile. I know it works for mex files. If this doesn't work, you can try this solution.

Community
  • 1
  • 1
m_power
  • 3,156
  • 5
  • 33
  • 54
2

How do you bind the S-Function to the DLL? If you bind at link time then you could try to bind at runtime and explicitly release the handle in mdlTerminate. That way, MATLAB should never even know that you accessed the DLL. Of course this makes the actual interaction with the DLL from within the S-Function somewhat messier.

Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
  • This is as close to an answer I have come to so far, using LoadLibrary() and FreeLibrary() being called on mdlStart and mdlTerminate respectively. The first attempt to use it caused a segmentation violation so am a bit nervous, but it has worked somewhat since then. – J Collins Dec 19 '13 at 09:28
  • @JCollins: Just make sure to keep your S-Function reentrant by storing the DLL handle inside a work vector (instead of using a global variable). Then you should be fine. – Florian Brucker Dec 19 '13 at 09:45