use loadlibrary with a C dll several times, then throw an error " Error using loaddefinedlibrary"

I use the follow syntex:

loadlibrary('CDLL', 'EXPORT.h', 'mfilename', 'mxCDLL');
unloadlibrary('CDLL');
for i=1:1000
    dll_name = 'CDLL';
    if ~libisloaded(dll_name)
        loadlibrary(dll_name, @mxCDLL);
    end
end

and when the loop proceed the 495 time, it stoped, and throw an error "

Error using loaddefinedlibrary".

please help me, thank you very much!

ps: the CDLL.dll is programed by CUDA C, and I just load it and do nothing else.

8 comentarios

As an experiment: inside the loop check
length(fopen('all'))
This should stay constant, but if loadlibrary() is doing an fopen() but missing the fclose() then it could be leaking file descriptors.
First of all, thank you very much, and I have try it, and the result is 0 constantly. So, what can I do else?

It would also be interesting to know how many times the ~libisloaded is true. Does it happen before iteration 495?

Is the iteration number of the error consistent?

Thank you Guillaume, I print out the value of "libisloaded", and it is always 0, so "~libisloaded" is ture, there is no difference between the last one and the before. Any other idea? and Walter Roberson?
I find it odd that libisloaded is consistently 0, so you are effectively reloaded it each time.
I still suspect you are running out of files -- but it could be the files are allocated internally when you load the library.

Yes, it is extremely odd that libisloaded is false. Did you expect that your library would take a long time to load? Is that why you wrote the loop?

Could it be that at iteration 495, the library is finally loaded and thus loadlibrary complains?

You may want to monitor what is actually happening at the OS level with for example procmon.

Does the initialisation function of your library try to acquire resources that may be unavailable (hence, why it'd take a long time to load)?

To Walter Roberson and Guillaume: This problem come from a work case, in that case, I need to load a C dll to acquire some function in MATLAB, but I found that when I load it several times later, it throw out execption. So I just write a test code that only loadlibrary and unloadlibrary, but do nothing with the dll function, and it gose wrong at the iteration 495. libisloaded is false, because I called unloadlibrary after loadlibrary, it is my mistake, I'm very sorry to do that. So, what can I do else? please give me some advise, thank you very much.

To avoid us following red herrings, can you give us the exact code that you're testing with?

I don't know if it would help, but can you share your dll?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Abr. de 2018

Comentada:

el 24 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by