LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

problem with .DLL and global variables

Hi,

I need some helps to figure out my problem.

I wrote a .dll file to open and run a GUI. When I run it at the first time, it is ok. When I call it again, there is an error "Pointer to free memory passed to library. It will be ok if I completely exit the program that calls the .dll before re-call it. I think there is some problems with global variables when I call another thread. How can I free global variables between threads. Is there any solution for this problem?

Thanks

0 Kudos
Message 1 of 5
(4,093 Views)

Vinh Pham:

 

I might be missing something here, but I don't know why you would ever want to free a global (unless maybe if it's a pointer as I discuss below).  If it's global, it should be available to anyone at anytime.  If it doesn't need to be always available, maybe it shouldn't be a global.

 

Freeing a global leads right to the type of problem you're seeing: after you free it, somebody tries to use it, but it's not there.  There is no way to recover a global once it has been free'ed.

 

If your global is a pointer, you can malloc()  and free() it as needed, but then you need to keep track of when it's free'ed, so you can malloc it before the next use.  If you're doing this, does it need to be global?

0 Kudos
Message 2 of 5
(4,081 Views)

Hi AI S,

 

Thanks for your respone.

 

I created a CUSTOMINPUT function that will call a graphical user interface (GUI) and when this function is called, the GUI appears. There are many callback functions associating with GUI and they use some common variables. This is the reason why I chose to set them as Global variables.

However, when I called the CUSTOMINPUT function again, it seems that the global variables were freed and I could not continue. To call CUSTOMINPUT again, I have to exit completely the program use .DLL containing CUSTOMINPUT.

 

Thanks

0 Kudos
Message 3 of 5
(4,073 Views)

Vinh Pham:

 

Are you doing anything intentionally that frees your globals?

 

CVI ships with an example program that demonstrates calling a DLL to create a user interface: guidll.cws.

 

I made two simple modifications to guidll.cws.

 

1. In guidll.c, I made hpanel global by moving its declaration from RunDLLUI() to right below the #include statements.

2. In useguidll.c, I duplicated the lines calling RunDLLUI () and MessagePopup() to call the DLL gui twice.

 

I built the project and ran it.  I had no problems with free'ed globals.

 

Take a look at the guidll example.

 

Can you either post your code or a modification to guidll that demonstrates the problem you're having?

0 Kudos
Message 4 of 5
(4,043 Views)

hi, Vinhpham

 

it just my assumption.

 

 you are dynamiclly load the dll from you application.  however, you may defined a global variable to store the pointer or handle which return from the dll.  and if you call it again. you may initial this global varialbe again and it point to other palce. it cab be a null point.

 

 

can you pls post you code here, it could help us to understand you problem correctly!

 

 

B.R

Gerry

0 Kudos
Message 5 of 5
(4,021 Views)