clear MEX not unloading DLLs in 2016b

Neither "clear all" or "clear mex" is unloading MEX DLLs. I can verify this by attaching to Matlab in the Visual C++ debugger and looking for DLL "unloading/loading" happening.
This has been asked before, with the following "answer" https://www.mathworks.com/matlabcentral/answers/100577-why-am-i-unable-to-delete-mex-functions-from-memory-using-the-clear-mex-command
It is unclear in this answer what is mean by memory leaks preventing MEX unloading? A MEX function , might have ,for example, have static C++ objects that allocate memory. That will only be de-allocated when the DLL is unloaded.
I remember in a previous version of Matlab , "clear MEX" would force all MEX DLL's to be unloaded.

2 commentaires

James Tursa
James Tursa le 1 Août 2018
Is this only a specific mex dll that you are having problems with?
Andrew Cunningham
Andrew Cunningham le 1 Août 2018
Modifié(e) : Andrew Cunningham le 1 Août 2018
These are our custom MEX DLLs that are a thin layer over other application specific DLLs. I am just trying to understand the restrictions/rules on unloading. Way back in Matlab 2007b, "clear mex" would definitely unload the DLLs. Now it doesn't.

Connectez-vous pour commenter.

Réponses (1)

Philip Borghesani
Philip Borghesani le 2 Août 2018
Modifié(e) : Philip Borghesani le 2 Août 2018

0 votes

Clear mex and clear all should still unload mex files that are not in use or locked (see mexLock ). The referenced answer is a bit vague, classic memory leaks will not prevent a mex file from unloading but some resource leaks might.
It is possible for back references or other OS activity to keep a mex file loaded even if MATLAB has attempted to unload it. Sometimes the OS or an antivirus will keep a dll locked for a period of time after MATLAB unloads it.
Check using inmem to see if MATLAB still has it locked. Process Explore (procexp.exe) can find if other applications have a dll locked.
You can also try using m mexAtExit to register an exit function that MATLAB will call before it releases it's last reference to a mex file. If clearing causes your exit function to be hit but the mex file is not unloaded then some other activity is keeping it loaded.

5 commentaires

Guillaume
Guillaume le 2 Août 2018
Andrew Cunningham comment mistakenly posted as an answer moved here:
Thanks for the suggestions but its still unclear why 'clear mex' does not unload my Mex DLL(s) and its dependent DLLs. My MEX files do not call mexLock()
The help for inmem says [...] = inmem('-completenames') returns not only the names of the currently loaded function and MEX-files, but the path and filename extension for each as well. No additional information is returned for loaded classes.
However, inmem('-completenames') does not return any information about my loaded MEX DLLs. It only lists information about Matlab functions. This actually is starting to look like a bug.
Also I am not sure why you would say "other applications have a DLL locked". Multiple applications might load the same DLL, but each application will load the DLL into their own unique process space. Matlab loading/unloading of DLLs happens completely independently from other processes.
Andrew Cunningham
Andrew Cunningham le 2 Août 2018
Following on from the suggestion, "process explorer" shows my MEX dlls 'owned' by Matlab and no other process. And as described 'clear mex' has no effect.
There is a bug or misunderstanding in how clear mex is working. I built the example "mexgetarray.c" from the MATLAB MEX examples using MATLAB 2018a.
Calling "clear mex" should unload the mex DLL and reset the internal counter. It does not.
>> mex mexgetarray.c
Building with 'Microsoft Visual C++ 2015 (C)'.
MEX completed successfully.
>> mexgetarray
mexgetarray has been called 1 time(s)
>> b=mexgetarray
mexgetarray has been called 2 time(s)
>> clear mex
>> b=mexgetarray
mexgetarray has been called 3 time(s)
Philip Borghesani
Philip Borghesani le 7 Août 2018
Modifié(e) : Philip Borghesani le 7 Août 2018
I think you missed somthing in the source code. The function stores the count in a matlab global and restores the count from there if cleared. See the if statement after the comment /* Check status of MATLAB and MEX-file MEX-file counter */. Use whos global to see the global and clear all, (or classes or globals) to clear the global.
> mexgetarray
mexgetarray has been called 1 time(s)
>> mexgetarray
mexgetarray has been called 2 time(s)
>> clear classes % clears mex and globals
>> mexgetarray
mexgetarray has been called 1 time(s)
Andrew Cunningham
Andrew Cunningham le 7 Août 2018
Yup, you are right. I missed that one.If I create another counter local to that routine, it does get reset to 0. So it seems simple mex files do get unloaded. But my more complex ones with dependent DLL's do not so that remains a mystery.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Produits

Version

R2016b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by