Unload MEX file from MATLAB

Hi, everybody!
I am writing and debugging a mex file in MATLAB. So every time I recompile it I need to reload it somehow in MATLAB. So far, the only things that works is to restart MATLAB, which is very inconvenient.
The way I tried already:
  1. clear mex (removes the file from [~,m] = inmem('-completenames'), but does not actually unload it)
  2. unloadlibrary 'name' (does not work with mex files)
  3. solution posted here: http://stackoverflow.com/questions/17296668/force-matlab-to-reload-library-linked-in-mex-function , which uses bdclose all; close all; rehash;
Are there any other reciepts, what one can do, or is it impossible?
Edit: After some testing, I really belive it is a problem of using shared libraries. I compiled the following MEX-OpenCV example:
#include <mex.h>
#include <opencv2/opencv.hpp>
void mexFunction( int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs){
mexPrintf("Number of input arguments: %d\n",nrhs);
mexPrintf("OpenCV version: %d.%d\n",CV_MAJOR_VERSION,CV_MINOR_VERSION);
}
if I compile it as
mex -lopencv_core testMEX.cpp
it is not possible to recompile it and use it directly after recompilation in MATLAB. However, if I compile the same code with
mex testMEX.cpp
it is loaded in MATLAB automatically directly after recompilation.

8 件のコメント

Kaustubha Govind
Kaustubha Govind 2013 年 8 月 8 日
編集済み: Kaustubha Govind 2013 年 8 月 8 日
Does your MEX-file load shared libraries? Are those what you need to unload? Where do you load the library?
defunct C7=5F>20
defunct C7=5F>20 2013 年 8 月 9 日
編集済み: defunct C7=5F>20 2013 年 8 月 9 日
> Does your MEX-file load shared libraries? Yes
> Are those what you need to unload? No, I don't need to unload shared libraries (they don't change), only mex file itself
> Where do you load the library? I don't understand the question. I load a mex file by executing it. Then I do some changes in the code (of the mex file) and recompile it. Now I need MATLAB to see the changes in the mex file and reload it, but it does not happen.
Ilya
Ilya 2013 年 8 月 9 日
Have you tried clear functions?
defunct C7=5F>20
defunct C7=5F>20 2013 年 8 月 11 日
No, this does not help. I found out, that if I create a very simple mex file that does not do anything (except printing some string), the mex is reloaded directly after recompilation. However, I don't see any memory leaks in my more complex mex file (the one, that is not reloaded after recompilation). So pointing out what are the rules of memory management for mex file would be also helpful.
dpb
dpb 2013 年 8 月 11 日
...
clear name1 ... nameN removes the variables, scripts, functions, or MEX-functions name1... nameN from memory.
clear removes debugging breakpoints in MATLAB files. If function name is locked by mlock, then it remains in memory.
The only reason one would think that clear isn't working is that you have either executed mlock or there's an error or a name resolution problem in the mex function.
What is the mex-file function name? What does
which _that_function_name_
return?
As for the question re: memory management; that seems to be out of leftfield--if you have memory leaks that's indicative of a problem in the mex file and that could easily I can imagine lead to difficulty in unloading a non-successfully completing function from memory.
The "rules" for mex files are given in the documentation for developing mex files. There's a specific topic on 'memory management' under 'matlab/matlab_external/memory-management'
defunct C7=5F>20
defunct C7=5F>20 2013 年 8 月 12 日
編集済み: defunct C7=5F>20 2013 年 8 月 12 日
I don't execute mlock in my mex explicitly. However, it can be executed by one of the side libraries I am using.
which func_name
returns the path to the mex file (even after executing clear func_name command)
Thank you for pointing out where to search info about the memory management, I will look at it. However, i checked my code and it does not seem that I have memory leaks somewhere there. MATLAB loads some other shared libraries, when executing my MEX, which are used by my MEX. It could also be a reason, but then not reloading the MEX itself looks more like a bag.
Alec Jacobson
Alec Jacobson 2018 年 6 月 10 日
Is there any followup to this problem with matlab? I'm suffering the same issue.
JulioLP
JulioLP 2021 年 11 月 12 日
Hi,
I had the same problem. Turns out a simple
clear all
is enough to unload the previous mex file and work with the new one. Not the most elegant solution, but it helps for debugging.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

質問済み:

2013 年 8 月 8 日

コメント済み:

2021 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by