Showing results for 
Search instead for 
Did you mean: 

How do we unload a DLL with multiple different CLFN?

Go to solution

Hi everybody,


I'm currently designing an application that implements multiple functions of a DLL that I build.

I have to call two functions one after the other to obtain the results of my functions.The first function (Read_Property) is doing many operations and saves its informations in a data layer inside the DLL.(Essentially some static arrays of string). The second one (get_Data_Array) is just a "get function" that returns the string at a defined position of an array.


The first version of my application was a state machine with a front panel where the user just had to enter the parameter of the function and press a button. I wasn't unloading DLL at any time because i thought that LabVIEW did it itself when I close the instance of the application. I got great results and everything was fine and I could do multiple call to my pair of functions without having any problems. However, I discovered a bug when I close the instance of my application and that I started another one. My first function got a Timeout for no apparent reason. I worked a lot with my DLL in a C context and I know that this kind of bug appears when multiple instances are running simultaneously. After having a timeout, the only thing I could do to fix this problem was to reboot LabVIEW.


So I think that my problem comes from the fact that there is interference in some static variables of the first function because that the DLL wasn't unloaded correctly. After realizing that, I tried to unload the dll in the last state of my state machine because it would clear all static variables, but it wasn't a success. The same problem was still present. After that, I tried to encapsulate my problem to a single VI. So I simply tried to execute my 2 functions one after the other and then unload the DLL by connecting an empty path to both of em. As for my first version, after two or three runs of my VI, i got a timeout again.


My VI looks like that:



Why does my DLL doesn't unload??


Thank you

Mathieu Veillette, Ing Jr.
0 Kudos
Message 1 of 13

Hi Mathieu,


Where do you give the Call Library Function Node the blank path to unload it from memory?

Tanya Visser
National Instruments
LabVIEW Group Manager
0 Kudos
Message 2 of 13

On the first run of my loop, i=0, so the path will be the "path In", on the second run, the CLFN will receive the Empty Path Constant.

Mathieu Veillette, Ing Jr.
0 Kudos
Message 3 of 13

Oh right. Sorry. I didn't look at your code too carefully.


Try adding a wait to your loop to give it more time to process.

Tanya Visser
National Instruments
LabVIEW Group Manager
0 Kudos
Message 4 of 13

You're right, I forgot to put one. However, it doesn't solved my problem.


I tried another thing this morning, and it seems to solve my problem. Instead of unloading the DLL after having done my operations, I tried to unload it before anything was done.


The VI now looks like this:



I don't know the intern behavior of the CLFN, but it seems to unload my DLL very well. I also ran the VI in the UI thread instead that in any thread (It wasn't working otherwise).


Any idea why it fixed the problem?

Mathieu Veillette, Ing Jr.
0 Kudos
Message 5 of 13

Well, Finally, it seems that my problem is not as solved as I thought.


I was just pressing the "run" button many times one after the other without closing my window. That way, we don't have a new instance. When I closed it, reopenned it and run again, I got my problem.


So it seems that my DLL weren't really closed, or something else. When I got this problem, I have to close LabVIEW and reopen it again.


Any idea?

Mathieu Veillette, Ing Jr.
0 Kudos
Message 6 of 13

Hi Mathieu,


The reason your last code isn't doing what you want is that the blank path needs to be wired to the same Call Library Function Node that you originally used to load the DLL.


Take a look at this KnowledgeBase article about loading and unloading DLLs dynamically.


Can I Dynamically Load and Unload a DLL in LabVIEW?


In this article, a 'Not A Path Constant' is used instead of an ' Empty Path Constant'. Try using the different constant and going back to something more like your original architecture, and let me know if this changes the behavior.


Take care!

Tanya Visser
National Instruments
LabVIEW Group Manager
0 Kudos
Message 7 of 13



The KnowledgeBase you posted is the first thing I looked out when I began 😉


I tried to change the "Empty Path constant" to a "Not a Path constant", but nothing changed.

I also change the DLL configuration to use the "UI thread" instead of "any". Once again, no change.


I tried to use other functions of my DLL and they seems to work fine, but their internal execution is less sensitive to interference than the Read_Property function. (The one I've got problem with since the beginning)


To be sure that the problem doesn't come from the DLL function, I did a small program in C that calls multiple instances of an executable version of this function. At each call, i explicitly load and unload DLL. When I do that, everything works like a charm.


I don't know the internal behavior of LabVIEW with static variables used in a DLL. Do the static variables of the DLL are stored in the main process memory(LabVIEW) and cleaned up only when LabVIEW is closed? I really doubt of it but well, I really don't know where to look right now.


Thanks for your support, it's really appreciated.


Mathieu Veillette, Ing Jr.
0 Kudos
Message 8 of 13
Accepted by topic author cheux

I've got some advancement here.


I just added some logging to see if the DLL was attach/detach, so I would know if the DLL has been unloaded or not.


After a plenty of tests, I realized that my first version was successfully Loading/unloading my DLL each time.





However, I still got my timeout and I bet it's because different threads are running my DLL at the same time.


I'll do another topic for my problem, because it's not the same subject.


Thank you for your help.

Mathieu Veillette, Ing Jr.
0 Kudos
Message 9 of 13

Hey folks.... Greetings...


I was having a look at this tread as i was trying something similar. I have a similar piece of code which uses a valid path or the "Not a Path" constant to load / unload the dll in one vi. However once the dll is loaded using a valid path, i have calls to numerous dll functions in multiple different vi's. Once the data has been acquired from the dll, i invoke the initial vi and pass in the "not a path" constant so that the dll gets unloaded (Hopefully).


Would this unload the dll correctly?


Also Mathieu, u said you did a lot of tests to confirm if the loading / unloading was successful. Could you please let me know how u tested this. I am using Labview 2012 on an XP system.


I am facing the issue of static variables in the dll not being destroyed and recreated when the next session of data acquision starts. (Sessions are started & terminated by the user pressing Start & Stop buttons on the UI).

0 Kudos
Message 10 of 13