Mex function not updated after recompile

前端 未结 2 450
长发绾君心
长发绾君心 2021-01-18 03:19

I have a simple mex function, which calls another C++ function from a library. I compile the source with

mex -cxx mymexfunction.cpp -I/some/include -L/some/l         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 03:57

    The mex command automatically clears the mex function if it is currently loaded in memory. Are you sure your mex function is closing whatever handle it holds to the other library? If such a handle exists it might prevent the OS from unloading the mex file.

    I've used the following set of commands to clear mex functions manually, and from my experience, using a full path to the mex file when calling clear works. So give this a try and if it still doesn't get unloaded you might want to start looking at the code for loading and unloading the other library.

    [~,f] = inmem( '-completenames' );
    result = strfind( f, ['mymexfile' '.' mexext] );
    result = f(cellfun( @isempty, result, 'UniformOutput', true ) == 0);
    clear( result{:} )
    

    Try running the inmem command again after the above and see if your mex file is still listed.

    Something that might help you with making sure the other library gets unloaded is maybe using an std::shared_ptr to hold the handle to this library. Then, at the beginning of the mexFunction() entry point load the library and stick the handle into the shared_ptr. The shared_ptr will also need to use a custom deleter to unload the library (on Windows the custom deleter would call FreeLibrary).

    Of course, if this is being caused by a bug in the other library none of this is going to help.

提交回复
热议问题